运行 time.sleep() 在另一个线程上冻结 Tkinter 程序

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)