等待线程完成时,tkinter GUI冻结/挂起

5

当我按下按钮时,我的界面会冻结。我正在使用线程,但是我不确定为什么它还是挂起了。任何帮助将不胜感激。提前致谢。

class magic:
    def __init__(self):
        self.mainQueue=queue.Queue()

    def addItem(self,q):
        self.mainQueue.put(q)

    def startConverting(self,funcName):
        if(funcName=="test"):
            while not self.mainQueue.empty():
                t = Thread(target = self.threaded_function)
                t.start()
                t.join()

    def threaded_function(self):

        time.sleep(5)
        print(self.mainQueue.get())

m=magic()
def helloCallBack():
   m.addItem("asd")
   m.startConverting("test")  //this line of code is freezing

B = tkinter.Button(top, text ="Hello", command = helloCallBack)

B.pack()
top.mainloop()

如果 m.startConverting("test") 是阻塞的,它可能会一直占用 GUI 线程,直到 join 返回。你可能需要在处理程序中启动线程。在 GUI 线程上执行长时间运行的任务会导致冻结。 - Carcigenicate
在处理程序中启动线程是什么意思?有例子吗? - Abdul Ahad
不要在 GUI 线程上创建新的线程并加入它,而是在程序开始时启动一个线程池,并在 startConverting 中提交作业。这样就不需要在 GUI 线程上阻塞 join 了。 - Carcigenicate
@Carcigenicate 有任何链接或示例吗? - Abdul Ahad
https://dev59.com/y3A75IYBdhLWcg3w6Nr8 - Carcigenicate
2个回答

6
这是一个关于使用基于tkinter的GUI执行异步任务的配方。我从引用书籍中的一份配方进行了改编。您应该能够修改它以满足您的需求。
为了保持GUI的响应性,需要干扰其mainloop(),例如通过像join()这样的后台线程来使GUI“挂起”,直到线程完成。这可以通过使用通用的after()小部件方法定期轮询Queue来实现。
# from "Python Coobook 2nd Edition", section 11.9, page 439.
# Modified to work in Python 2 & 3.
from __future__ import print_function

try:
    import Tkinter as tk, time, threading, random, Queue as queue
except ModuleNotFoundError:   # Python 3
    import tkinter as tk, time, threading, random, queue

class GuiPart(object):
    def __init__(self, master, queue, end_command):
        self.queue = queue
        # Set up the GUI
        tk.Button(master, text='Done', command=end_command).pack()
        # Add more GUI stuff here depending on your specific needs

    def processIncoming(self):
        """ Handle all messages currently in the queue, if any. """
        while self.queue.qsize():
            try:
                msg = self.queue.get_nowait()
                # Check contents of message and do whatever is needed. As a
                # simple example, let's print it (in real life, you would
                # suitably update the GUI's display in a richer fashion).
                print(msg)
            except queue.Empty:
                # just on general principles, although we don't expect this
                # branch to be taken in this case, ignore this exception!
                pass


class ThreadedClient(object):
    """
    Launch the main part of the GUI and the worker thread. periodic_call()
    and end_application() could reside in the GUI part, but putting them
    here means that you have all the thread controls in a single place.
    """
    def __init__(self, master):
        """
        Start the GUI and the asynchronous threads.  We are in the main
        (original) thread of the application, which will later be used by
        the GUI as well.  We spawn a new thread for the worker (I/O).
        """
        self.master = master
        # Create the queue
        self.queue = queue.Queue()

        # Set up the GUI part
        self.gui = GuiPart(master, self.queue, self.end_application)

        # Set up the thread to do asynchronous I/O
        # More threads can also be created and used, if necessary
        self.running = True
        self.thread1 = threading.Thread(target=self.worker_thread1)
        self.thread1.start()

        # Start the periodic call in the GUI to check the queue
        self.periodic_call()

    def periodic_call(self):
        """ Check every 200 ms if there is something new in the queue. """
        self.master.after(200, self.periodic_call)
        self.gui.processIncoming()
        if not self.running:
            # This is the brutal stop of the system.  You may want to do
            # some cleanup before actually shutting it down.
            import sys
            sys.exit(1)

    def worker_thread1(self):
        """
        This is where we handle the asynchronous I/O.  For example, it may be
        a 'select()'.  One important thing to remember is that the thread has
        to yield control pretty regularly, be it by select or otherwise.
        """
        while self.running:
            # To simulate asynchronous I/O, create a random number at random
            # intervals. Replace the following two lines with the real thing.
            time.sleep(rand.random() * 1.5)
            msg = rand.random()
            self.queue.put(msg)

    def end_application(self):
        self.running = False  # Stops worker_thread1 (invoked by "Done" button).

rand = random.Random()
root = tk.Tk()
client = ThreadedClient(root)
root.mainloop()

在 Python 3.x 上运行此代码将在单击“完成”按钮时引发异常。实际上,执行 sys.exit(1) 将冻结 GUI。 - Federico Dorato
@Federico:当我运行它(Windows和Python 3.8.2)时,它不会这样做。它会持续打印线程放入队列的随机数,直到我按下“完成”按钮,此时它会优雅地退出而没有异常。 - martineau
Wojceich说:“对于在@martineau的代码中遇到sys.exit(1)问题的任何人 - 如果您将sys.exit(1)替换为self.master.destroy(),程序会优雅地结束。我缺乏声望来添加评论,因此是单独的答案。” - Delrius Euphoria
1
@CoolCloud:正如sys.exit(1)的注释所指出的那样 # This is the brutal stop of the system. You may want to do some cleanup before actually shutting it down,因此销毁master肯定会合格,并且是可移植的,得到了我的全力支持,无论怎样,我在我的系统上不需要它。感谢您发布@Wojciech的建议。 - martineau
如果你需要处理多个小部件方法并在线程上运行代码,该怎么办?你会将每个方法传递给GuiPart并在那里附加监听器吗?这似乎非常笨拙。 - Thegerdfather

4

如果您在@martineau的代码中遇到sys.exit(1)问题 - 如果您将sys.exit(1)替换为self.master.destroy(),程序将正常结束。由于我的声誉不够高不能添加评论,因此提供此独立答案。


1
我已经在那里添加了,感谢分享:D - Delrius Euphoria
1
Wojciech:总的来说,这是一些好的通用建议,即使不是在所有系统上都必要。 - martineau

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接