2014-03-27 63 views
1

嗨我已經創建了一個使用tkinter的GUI,它接受來自用戶的輸入。 我已經創建了自己的存在檢查和「拼寫檢查」,以確保用戶只輸入可接受的值。 但每次輸入這些值時,它都會讓用戶在輸出屏幕出現之前多次單擊提交按鈕。 這是因爲我使用了多個功能? 有沒有其他方法可以做到這一點?爲什麼我必須多次點擊按鈕? tkinter gui

def Search(): 
    global E1, E2, E3, E4, file 

    def PresenceValidation(): 

     if E1.get() ==(''): 
      root2=Tk() 
      errortext = Label(root2, text = "Please enter eye colour") 
      errortext.pack() 
     elif E2.get() ==(''): 
      root2=Tk() 
      root2.configure(bg="white") 
      errortext = Label(root2, text = "Please enter age") 
      errortext.pack() 
     elif E3.get() ==(''): 
      root2=Tk() 
      root2.configure(bg="white") 
      errortext = Label(root2, text = "Please enter hair colour") 
      errortext.pack() 
     elif E4.get() ==(''): 
      root2=Tk() 
      root2.configure(bg="white") 
      errortext = Label(root2, text = "Please enter shoesize") 
      errortext.pack()    
     else: 
      button = Button(root, text = "Submit information", command=SpellCheck) 
      button.grid(row=3, column=2, padx = 5) 

    def SpellCheck(): 
     if E1.get().lower() not in ('blue', 'brown', 'green'): 
      root2=Tk() 
      root2.configure(bg="white") 
      errortext = Label(root2, text = "Please enter eye colour") 
      message= Label(root2, text = "Blue, Brown or Green") 
      errortext.pack() 
      message.pack() 
     elif E2.get().lower() not in ('10-20', '20-30', '30-40','40+'): 
      root2=Tk() 
      root2.configure(bg="white") 
      errortext = Label(root2, text = "Please enter a valid age group") 
      message=Label(root2, text = "10-20, 20-30, 30-40 or 40+") 
      errortext.pack() 
      message.pack() 
     elif E3.get().lower() not in ('Brown', 'Black', 'Blonde', 'Auburn'): 
      root2=Tk() 
      root2.configure(bg="white") 
      errortext = Label(root2, text = "Please enter a valid hair colour") 
      message = Label(root2, text = "Brown, Black, Blonde or Auburn") 
      errortext.pack() 
      message.pack() 
     elif E4.get().lower() not in ('1', '2', '3','4','5','6','7','8+'): 
      root2=Tk() 
      root2.configure(bg="white") 
      errortext = Label(root2, text = "Please enter a valid shoesize") 
      message = Label(root2, text = "1,2,3,4,5,6,7,8+") 
      errortext.pack() 
      message.pack()   
     else: 
      submitbutton = Button(window, text = "Submit information", command=Submit) 
      submitbutton.grid(row=3, column=2, padx = 5) 


    root = Tk() 
    root.title ("Search for matches") 



    #text that will be shown to the user 
    root.configure(bg="white") 
    Label(root, text="Please enter eye colour").grid(row=0) 
    Label(root, text="Please enter age").grid(row=1) 
    Label(root, text="Please enter hair colour").grid(row=2) 
    Label(root, text="Please enter shoesize").grid(row=3) 


    #Create user entry points 
    E1= Entry(root) 
    E2= Entry(root) 
    E3= Entry(root) 
    E4= Entry(root) 
    # locating input boxes to area on the screen 
    E1.grid(row=0, column=1) 
    E2.grid(row=1, column=1) 
    E3.grid(row=2, column=1) 
    E4.grid(row=3, column=1) 




    submitbutton = Button(window, text = "Submit information", command=PresenceValidation) 
    submitbutton.grid(row=3, column=2, padx = 5) 

    def Quit(): 
    window.destroy() 

    quitbton = Button (window, text ="QUIT", command=Quit) 
    quitbton.grid(row=3, column=3, padx=5) 
+0

你有2個按鈕完全相同的文本'提交信息'&位置,甚至'submitbutton'變量 – Gogo

+0

這是因爲他們都導致相同的輸出功能 – user3368422

+0

事情是他們在完全相同的位置,所以他們重疊彼此。現在你必須不得不按下按鈕兩次,因爲這 – Gogo

回答

0

你可以重寫這個來省略很多代碼,並且仍然按照你想要的方式運行它。你得到多個按鈕的原因是因爲你正在製作多個按鈕。其中三個,如果我把他們全部計算在內。最初的Submit按鈕應該調用一個函數來驗證這些字段,如果它運行的值不正確,則會跳出該循環。您也可以使用Messagebox來提示用戶錯誤的位置。

如果您沒有添加Messagebox並希望保持原樣,則應該將root2實例更改爲Toplevel小部件,以避免創建兩個Tkinter實例。這也可能導致意想不到的結果。

這裏是你如何能簡單的讓你的存在檢查一個例子:你的問題的

from tkMessageBox import showwarning 

def PresenceValidation(): 
    #Make dictionary to hold values 
    values = {'Eye color': E1.get(), 
       'Age': E2.get(), 
       'Hair': E3.get(), 
       'Shoesize': E4.get() 
       } 

    #Iterate over dictionary to check for empty items 
    for k,v in values.iteritems(): 
     if v == '': 
      showwarning('Empty Value Alert', #Create alert message 
         k + ' needs a value') #for the first empty value, 
      break        #and break out of the loop 
0

部分原因是要創建一個以上的根窗口。 Tkinter的設計並非如此。如果您需要更多窗口,請創建Toplevel的實例。

相關問題