Python Tkinter: How do I make my GUI responsive as long as a thread runs?

大城市里の小女人 提交于 2021-02-07 08:37:00

问题


For example:

import threading
import time
import Tkinter


class MyThread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        print "Step Two"
        time.sleep(20)

class MyApp(Tkinter.Tk):

    def __init__(self):
        Tkinter.Tk.__init__(self)

        self.my_widgets()

    def my_widgets(self):
        self.grid()

        self.my_button = Tkinter.Button(self, text="Start my function",
                                          command=self.my_function)
        self.my_button.grid(row=0, column=0)

    def my_function(self):
        print "Step One" 

        mt = MyThread()
        mt.start()

        while mt.isAlive():
            self.update()

        print "Step Three"

        print "end"

def main():
    my_app = MyApp()
    my_app.mainloop()

if __name__ == "__main__":
    main()

Well, if I start my example it works as expected. I click on a button, my_function starts and the GUI is responsive. But I have read that I should avoid the use of update(). So, it would be nice if some one could explain why and how I have to wait for a thread correctly? Step Two is in a thread because it takes a much longer time than Step One and Step Three, otherwise it would block the GUI.

I'm new to Python and I trying to write my first "program". Maybe I'm thinking in a wrong way since I'm not very experienced...

Regards, David.


回答1:


You need to remember that you have an event loop running, so all you need to do is check on the thread each time the event loop makes an iteration. Well, not every time, but periodically.

For example:

def check_thread(self):
    # Still alive? Check again in half a second
    if self.mt.isAlive():
        self.after(500, self.check_thread)
    else:
        print "Step Three"

def my_function(self):
    self.mt = MyThread()
    self.mt.start()
    self.check_thread()


来源:https://stackoverflow.com/questions/11148795/python-tkinter-how-do-i-make-my-gui-responsive-as-long-as-a-thread-runs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!