2013-12-10 53 views
2

當我多次按下「Enter」按鈕時,文本字段將繼續顯示,但我需要它,這樣一旦我輸入了我的名字並按下按鈕,該按鈕就會停用。我該怎麼做呢?如何驗證tkinter中的按鈕,以便該函數只被調用一次?

class Enter_Name_Window(tk.Toplevel): 
    '''A simple instruction window''' 
    def __init__(self, parent): 
     tk.Toplevel.__init__(self, parent) 
     self.text = tk.Text(self, width=40, height=2) 
     self.text.pack(side="top", fill="both", expand=True) 
     self.text.insert("end", "Please enter your name and class. ") 

     enter_name = Entry(self) 
     enter_name.pack() 

     enter_name.focus_set() 

    def callback(): 
     self.display_name = tk.Text(self, width=40, height=2) 
     self.display_name.pack(side="top", fill="both", expand=True) 
     self.display_name.insert("end", "Now please enter your tutor group.") 
     tutor = Entry(self) 
     tutor.pack() 

     tutor.focus_set() 

     Enter_0_2 = Button(self, text="Enter", width=10, command=callback2) 
     Enter_0_2.pack() 

    def callback2(): 
     self.display_name = tk.Text(self, width=40, height=2) 
     self.display_name.pack(side="top", fill="both", expand=True) 
     self.display_name.insert("end", "Let's begin! Exit back to main screen.") 


    Enter_0 = Button(self, text="Enter", width=10, command=callback) 
    Enter_0.pack() 

回答

1

使用state="disabled"按鈕的config()方法來禁用一個小部件。您可以使用state="normal"以類似的方式啓用禁用的小部件。

根據你的樣本代碼的示例:

import Tkinter as tk 


class Application(tk.Frame): 

    def __init__(self, master): 
     tk.Frame.__init__(self, master) 
     self.text = tk.Text(self, width=40, height=2) 
     self.text.pack(side="top", fill="both", expand=True) 
     self.text.insert("end", "Please enter your name and class. ") 

     enter_name = tk.Entry(self) 
     enter_name.pack() 
     self.enter_0 = tk.Button(self, text="Enter", width=10, command=self.callback) 
     self.enter_0.pack() 

     enter_name.focus_set() 
     self.pack() 

    def callback(self): 
     self.display_name = tk.Text(self, width=40, height=2) 
     self.display_name.pack(side="top", fill="both", expand=True) 
     self.display_name.insert("end", "Now please enter your tutor group.") 
     tutor = tk.Entry(self) 
     tutor.pack() 
     tutor.focus_set() 
     self.enter_0.config(state="disabled") 

     Enter_0_2 = tk.Button(self, text="Enter", width=10, command=self.callback2) 
     Enter_0_2.pack() 

    def callback2(self): 
     self.display_name = tk.Text(self, width=40, height=2) 
     self.display_name.pack(side="top", fill="both", expand=True) 
     self.display_name.insert("end", "Let's begin! Exit back to main screen.") 

root = tk.Tk() 
root.geometry("600x400") 
app = Application(root) 
app.mainloop() 
+1

「活性」應當被只有當鼠標指針懸停按鈕,否則使用「正常」是正確的狀態。 –

+0

@BryanOakley感謝您的糾正,我已經更新了答案。 – Fiver

相關問題