[英]Running time.sleep() on another thread freezes Tkinter program
So I am trying to run time.sleep() on another thread, but the my entire program still freezes upon calling the function.所以我试图在另一个线程上运行 time.sleep(),但我的整个程序在调用该函数时仍然冻结。 I call it using a button.
我使用按钮调用它。 This is to create an error, and then wait a couple seconds to remove the error.
这是为了创建错误,然后等待几秒钟以消除错误。 Not sure what else to do here.
不知道在这里还能做什么。
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()
Any ideas would be awesome!任何想法都会很棒!
I figured it out.我想通了。 All that I had to do was use a different function inside threading.
我所要做的就是在线程中使用不同的函数。
Swap out:换出:
t = threading.Thread(target=self.removeError())
t.start()
To:至:
threading._start_new_thread(self.removeError, ())
And that's it.就是这样。 Works like a charm!
像魅力一样工作!
You're calling self.removeError and then you're using the result as target
.您正在调用 self.removeError ,然后将结果用作
target
。
So instead of:所以而不是:
t = threading.Thread(target=self.removeError())
you have to do this:你必须这样做:
t = threading.Thread(target=self.removeError)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.