2017-10-07 49 views
0

我想用tkinter編寫一個簡單的定時器。 我有一個啓動按鈕:python3 + tkinter:當點擊執行方法時,按鈕沒有被禁用

start_button = tkinter.Button(root, bg="white", text="Start", command=start_button_clicked) 

和上點擊運行

def start_button_clicked(): 
    start_button.config(text='Started', state='disabled') 
    tm = timer.Timer() 
    tm.count_time(1) 

我預期

  1. 更改按鈕文本和狀態
  2. 創建新的定時器和一個命令運行倒計時

但事實上,按鈕參數只有在計時器用完後纔會更改。 爲什麼會發生這種情況,點擊後如何更改bu按鈕?

回答

0

我遇到了麻煩timer.Timer()得到認可,但如果你想後立即更改按鈕的狀態你config add在root.update()

def start_button_clicked(): 
    start_button.config(text='Started', state='disabled') 
    root.update() 
    tm = timer.Timer() 
    tm.count_time(1) 
+0

它是我自己的自定義計時器。所以這並不奇怪,它不起作用。你的建議幫了很大忙,謝謝! – dariaamir

0

您正在使用Timer()函數不正確。您可以嘗試使用「時間」庫創建計時器,或使用time.delay(10)。但是,如果你想使用Timer(),你CA執行以下操作:

from threading import Timer 
from tkinter import * 


def disable_button(): 
    start_button.config(text='Started', state='disabled') 

def start_button_clicked(): 
    tm = Timer(3, disable_button) 
    tm.start() 

root = Tk() 
root.minsize(100, 100) 

start_button = tkinter.Button(root, bg="white", text="Start", command=start_button_clicked) 
start_button.pack() 

root.mainloop()