2017-10-07 113 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()