【发布时间】:2018-07-27 18:48:59
【问题描述】:
我正在制作一个使用按钮小部件的 tkinter 代码,但是当我按下按钮时,它会一直被按下,直到按下按钮时执行的功能没有完成。我希望立即释放按钮并执行该功能。 这是一个代码,显示了发生的一个很好的例子:
from tkinter import *
import time
root = Tk()
root.geometry('100x100+100+100') # size/position of root
def callback(): # this function will run on button press
print('Firing in 3')
time.sleep(3) # wait for 3 seconds
def main(): #function 'main'
b = Button(root, text="ᖴIᖇE", width=10,height=2, command=callback)# setting the button
b["background"] = 'red' #button color will be red
b["activebackground"] = 'yellow' #button color will be yellow for the time when the button will not be released
b.place(x=25,y=25) #placing the button
main() # using function 'main'
mainloop()
【问题讨论】:
-
你应该使用线程。
-
按钮卡住取决于代码。如果你真的有睡眠,你应该改用多线程或
after。如果实际过程需要很长时间,您可以使用更新方法或多线程。 -
嗯,你让它睡觉,所以它在睡觉。休眠时无法更新显示。
-
多线程不是答案。请。
-
tkinter 是单线程的——一次只能做一件事。当你的循环运行时,它不能更新显示。在 stackoverflow 上有很多个问题。
标签: python python-3.x button tkinter