[英]Tkinter button stays pressed
我正在制作一個使用按鈕小部件的 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()
GUI 程序通常在單個線程中驅動,由使用中的圖形工具包的“主循環”控制。 也就是說:程序通常會設置應用程序,並將控制權傳遞給工具包,該工具包運行一個緊密循環來回答所有用戶(以及網絡、文件等)事件,並且是唯一再次運行的用戶代碼是在設置階段編碼的回調。
同時,當您的代碼在回調期間運行時,它持有控制權——這意味着當您的函數沒有返回時,工具包將無法響應任何事件。
必須做的是編寫與 GUI 工具包協作的代碼 - 也就是說,如果您需要時間間隔的事物,則創建生成進一步回調的事件。 在 tkinter 的情況下,這是通過小部件的.after
方法實現的:經過這么多毫秒后,將運行傳遞的可調用對象。 time.sleep
,而另一方面,停止單線程那里,事件循環不運行。
在您的示例中,您可以簡單地編寫:
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')
root.after(3000, realcallback)
def realcallback():
print('Firing now!')
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()
我想補充:
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
root.update() #<- this works for me..
print('Firing in 3')
root.after(3000, realcallback)
def realcallback():
print('Firing now!')
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()
您可以在按下時被阻止的函數內使用threading
。 這是我對你的編輯:
from tkinter import *
import time
import threading
root = Tk()
root.geometry('100x100+100+100') # size/position of root
def callback(): # this function will run on button press
def callback2():
print('Firing in 3')
time.sleep(3) # wait for 3 seconds
threading.Thread(target=callback2).start()
def realcallback():
print('Firing now')
def main(): # function 'main'
b = Button(
root,
text="Fire",
width=10,
height=2,
command=callback
) # setting the button
b["background"] = 'red' # button color will be red
# button color will be yellow for the time when the button will not be released
b["activebackground"] = 'yellow'
b.place(x=25, y=25) # placing the button
main() # using function 'main'
mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.