21

I have a program that runs a long process after you click an action button. As the process is running the root window will say that it is not responding even though I know the program is running in the background. This program is going to be released to a few people that I work with and I want to make sure they don't freak out and close the window when they see this. The solution I have is sitting a root.update in the loop of the process that is running but I am not sure this was the best fix.

Using the python 3.3

Here is a sample of the code so you get an idea of what I am doing, this is called from the main loop:

def combine(boxes_to, boxes_from, frame):
        to_value,to_pos = gui.checkBoxes(boxes_to)
        from_value,from_pos = gui.checkBoxes(boxes_from)
        frame.destroy()

        running = Label(root,text="Running please do not close..",font = (16))
        running.pack()
        root.update()
        map_to = open("map_to",'r')
        for line in map_to:
            root.update()
            process(line)


        running.destroy()
        map_to.close()
        finish = Button(root, text="Done",command=gui.stop)
        finish.pack()
Eric Thomas
  • 667
  • 1
  • 8
  • 18
  • 2
    You should run the background process in another thread. Your single-threaded program can only do one thing at a time. When your long running process is running the UI can't keep up. – korylprince Aug 29 '13 at 23:04

2 Answers2

18

While you can call root.update() in your loop, this will still produce some (potentially) undesirable side-effects.

  1. The program may act laggy, meaning it takes a long time to respond to user input.
  2. You will only be able to run this one action. Any other action has to wait for this to finish.

As an alternative I would suggest that you implement simple multi-threading. Python multithreading is pretty simple, and will prevent both of these drawbacks. You will be able to execute your long running code, while still providing a clean and responsive UI.

If your application is trivially parallelizable, you could use multiple threads to decrease running time. Ex. Thread 1 handles entries 1-100, while thread 2 handles entries 101-200.

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
Daisetsu
  • 4,846
  • 11
  • 50
  • 70
  • 1
    Just a note: Accessing `tkinter` widgets/variables from different threads is bad practice. Sometimes `tkinter` can crash without an error message. Only some parts of `tkinter` are meant to be thread safe. – TheLizzard Jun 13 '21 at 23:37
  • My program runs for a half hour and I want the user to get progress updates -- root.update() was the trick!! – Robert Forderer May 03 '22 at 03:08
14

The best you can do here is to use multithreading in Python. Here's how to do this:

Let's say you have a function named combine() due to which the window is freezing, which is being used as a command for a button named 'btn' as shown here:

btn = Button(root, text="Click Me", command=combine)

Now, when btn is pressed you might be getting the 'not responding' problem. To fix this, edit the code as shown below:

import threading
btn = Button(root, text="Click Me", command=threading.Thread(target=combine).start)

Here threading.Thread creates a separate thread in which the combine() method is executed, so the GUI can continue to keep responding while the command is being executed.

wovano
  • 4,543
  • 5
  • 22
  • 49
Prashantzz
  • 301
  • 2
  • 7