[英]Running time.sleep() on another thread freezes Tkinter program
所以我試圖在另一個線程上運行 time.sleep(),但我的整個程序在調用該函數時仍然凍結。 我使用按鈕調用它。 這是為了創建錯誤,然后等待幾秒鍾以消除錯誤。 不知道在這里還能做什么。
import threading, time
class Block():
def createError(self, text):
background.itemconfig(errorText, text=text, state=NORMAL, font="Neoteric 11 bold")
background.itemconfig(errorBG, state=NORMAL)
# Mainly this part to worry about
t = threading.Thread(target=self.removeError())
t.start()
print("Function called") # Only prints AFTER 5 seconds, even though removeError() should be running on another thread
def removeError(self):
time.sleep(5)
background.itemconfig(errorText, text="", state=HIDDEN)
background.itemconfig(errorBG, state=HIDDEN)
# Tkinter stuff here to create button and run createError()
任何想法都會很棒!
我想通了。 我所要做的就是在線程中使用不同的函數。
換出:
t = threading.Thread(target=self.removeError())
t.start()
至:
threading._start_new_thread(self.removeError, ())
就是這樣。 像魅力一樣工作!
您正在調用 self.removeError ,然后將結果用作target
。
所以而不是:
t = threading.Thread(target=self.removeError())
你必須這樣做:
t = threading.Thread(target=self.removeError)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.