Question

Using the ttk.Progressbar in Python (in a tkinter GUI), I'm trying to have a progressbar appear when I press a button. Clicking the "Analyse" button on the gui calls the function analyze in the script, which I've meant to be a three-stage rocket of sorts:

  1. grid() the progressbar-widget to add it to the mainframe, and start() it.

  2. Do the actual computing (which takes about three seconds in my test-runs)

  3. stop() the progressbar and grid_remove() it to hide it from the user again, now that the job is complete.

If I comment out step 3, the progressbar will be shown and started as expected, while step 2 is executed. But if step three remains, the progressbar is never shown in the first place. Now, since the work in step two is being executed either way, I'm assuming that it might have something to do with gridding and removing of the progressbar-widget in the same function-call, but since this is my first real gui project in python, I'm a bit at a loss on how to work around that (if that indeed is the problem). Is there a way around this, or a way of more elegantly accomplishing the same task? The function the button calls is as follows:

def analyze():
    # --Step 1--
    progressbar.grid()
    progressbar.start()

    # --Step 2--
    global analysis
    analysis = analyzer.Analysis(file_path.get())
    analysis.output_to_file()

    # --Step 3--
    progressbar.stop()
    progressbar.grid_remove()

I'd post a picture of my interface to clarify further, but my since my current reputation score does not allow posting of images, I hope I've made the problem clear enough with my explanation (and code).

Was it helpful?

Solution

In order for the a widget to appear after being created, the event loop must be allowed to run. That is because the actual drawing of the widget on the screen happens as a result of a redraw event.

You can call update_idletasks which will allow the event loop to service such redraw events, after creating and starting the progress bar. However, while your calculation is running your app will likely appear to be frozen. Again, this is because the event loop must be allowed to service events, which it can't do when doing other things.

The normal way to solve this problem is to run your long running calculation in a thread or separate process.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top