2014-06-12 49 views
1

我正在使用python GUI(TK)。我有一個運行很長程序的按鈕 - 但我希望在點擊它之後立即禁用它。 它看起來像這樣:使用Tk按鈕的Python

button = tk.Button(self, text="blabla", command= lambda: self.foo(param, button) 
def foo(self, button): 
     button.configure (state = "disabled") 
     #now call the function that takes time 
     goo() 
def goo(): 
    doLongAction() 

問題是按鈕僅咕後禁用()返回,接着foo的回報。 有沒有辦法立即禁用它? 感謝

+0

您應該解決您的壓痕,以便更容易看到發生了什麼事情。 – Brionius

+0

我編輯它,所以它會更容易理解 – user2950329

回答

3

你需要通過調用update_idletasks方法禁用它後更新按鈕控件:

button = tk.Button(self, text="blabla", command= lambda: self.foo(param, button) 
def foo(self, button): 
     button.configure (state = "disabled") 
     ########################## 
     button.update_idletasks() 
     ########################## 
     #now call the function that takes time 
     goo() 
def goo(): 
    doLongAction() 
+0

謝謝,但我不知道如何使用它...我應該如何修改我的代碼上面?我應該離開命令爲富還是改變它? – user2950329

+0

@ user2950329 - 當你禁用按鈕時,你會在行後面調用'update_idletasks'方法。看我的編輯。 – iCodez

+0

作品!非常感謝你!! – user2950329