开发者

How do I make a button in my tk UI that makes the program do a whole ton of stuff when it is pressed not freeze the window?

开发者 https://www.devze.com 2023-03-21 03:17 出处:网络
I\'m just getting started with Tk and using it in python.I set up a button that does a ton (like two minutes worth) of stuff behind the scenes when you press it.I also set up a status text to show wha

I'm just getting started with Tk and using it in python. I set up a button that does a ton (like two minutes worth) of stuff behind the scenes when you press it. I also set up a status text to show what's going on while this is happening. I set it up like th开发者_JAVA技巧is:

status_text = Tkinter.StringVar()
ttk.Button(mainframe, text="Stats!", command=go).grid(column=1, row=4)

def go(*args):
    status_text.set("Logging in...")
    do_lots_of_stuff()
    status_text.set("Doing stuff #1...")
    do_even_more_stuff()
    status_text.set("Success!")

The problem is that when you press that button the window freezes, and the status text never actually changes. It looks broken, and doesn't come out of this state until all the processing finishes 2-3 minutes later. How do I make it not freeze and update the status text?


It's time to learn multithreading!

What's happening is that the GUI (main thread) is waiting for the method to return so that it can continue updating the interface.

You'll want to cause the action of a button to spawn a threading.Thread instead of running the heavy code in the main thread. You'll also want to create a Queue to access the data from the other thread (since sending GUI requests should ONLY be made from the main thread).

import threading, Queue #queue in 3.x

my_queue = Queue.Queue()

def go(*args):
    my_thread = threading.Thread(target=function_that_does_stuff)

def function_that_does_stuff():
    my_queue.put("Logging in...")
    do_lots_of_stuff()
    my_queue.put("Doing stuff #1...")
    do_even_more_stuff()
    my_queue.put("Success!")

Then you'll need a function that is run when the update event happens.

def OnUpdate(*args):
    try:
        status_text.set(my_queue.get())
    except Queue.Empty:
        pass


If you have control of do_lots_of_stuff and you can break it into small chunks, you can place small jobs on the event queue to do each chunk.

For example, if you're do_lots_of_stuff is reading lines from a file, create a method that reads one line and then puts a job on the event queue to call itself after a ms or two. This way the event loop continues to run, and your code does a little processing on each iteration. This is a surprisingly effective technique, though it only works if you're able to break your 'lots of stuff' into atomic chunks.

If you can't do that you'll have to either use multiple threads or multiple processes. I personally prefer the latter -- multiprocessing code is (arguably) less difficult to write and debug.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号