繁体   English   中英

在另一个线程上运行 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM