0

I am making a GUI for a project and my supervisor requested that I include a progress bar as each iteration of the program can take up to 7 minutes.

I want the progress bar to pop up in a separate window, and close after the process is finished but, with my current understanding, either I have to close the popup manually or the whole GUI closes at once.

This is a simplified version of the code I'm using

import Tkinter as tk
from ttk import Progressbar
from os import listdir
import threading
import time
root = tk.Tk()
root.title("TCC Image Processing")



def create_tcc(input_bar, input_progress_dialog):


    input_bar['maximum'] = 5
    input_bar['value'] = 0
    for x in range(5):
        time.sleep(1)
        input_bar['value'] += 1
    # this line is where the touble seems to be
    input_progress_dialog.destroy()


def run_tcc():
    progress_dialog = tk.Toplevel()
    progress_dialog.title("TCC Processing")

    bar = Progressbar(progress_dialog, orient="horizontal", length=500, value=0, mode="determinate")
    bar.grid(row=4, columnspan=2)
    t = threading.Thread(target=create_tcc, args=(bar, progress_dialog))
    t.start()
tcc_run_button = tk.Button(root, text="RUN", command=lambda:run_tcc())
tcc_run_button.pack()

root.mainloop()

If possible I would like to avoid downloading extra modules that don't come with straight python.

cforeman
  • 59
  • 1
  • 6
  • Aside: the similarity of the names global `root`, `run_tcc.root2`, and `create_tcc.root` is confusing. Since neither of the local variables actually refer to the app's primary window, maybe choose another variable name, like `progress_dialog`? – Robᵩ Nov 03 '16 at 14:57
  • Your code, in its current edited form, works perfectly for me. Precisely what problem do you have with it now? – Robᵩ Nov 03 '16 at 15:38
  • The code above runs, and when I click the button a popup appears with a progress bar. The bar steps four times, and then stops, when it should be stepping five times. The only time it stepped five times with the current version of the code the program crashed, and a new popup appeared saying a problem was encountered. – cforeman Nov 03 '16 at 15:48
  • I think the program crashes because `tkinter` is single threaded and you are trying to put it into two threads – Delrius Euphoria Jun 23 '21 at 15:18

2 Answers2

2

Call .destroy() to progress_dialog after the task is finished.

progress_dialog.destroy()

This will safely quit the popup without killing the whole application.

BernardoGO
  • 1,826
  • 1
  • 20
  • 35
0

Sorry to necro but I came across this and found the answer and wanted to share in case others run into it.

At the exact moment the progress bar reaches its 5 second lifespan the window closes. The bar does load to 100%, you might need to take a recording and slow it down, but it definitely hits 100%. The problem is that because the window hits 100% at the exact same millisecond it gets destroyed, it looks like it doesn't make it.

Adding time.sleep(.5) before your input_progress_dialog.destroy() fixes the perceived failure point.

Code below is for Python 2, for 3, change import Tkinter as tk to import tkinter as tk and from ttk import Progressbar to from tkinter.ttk import Progressbar

import Tkinter as tk
from ttk import Progressbar
from os import listdir
import threading
import time
root = tk.Tk()
root.title("TCC Image Processing")



def create_tcc(input_bar, input_progress_dialog):


    input_bar['maximum'] = 5
    input_bar['value'] = 0
    for x in range(5):
        time.sleep(1)
        input_bar['value'] += 1
    # this fixes it
    time.sleep(.5)
    input_progress_dialog.destroy()


def run_tcc():
    progress_dialog = tk.Toplevel()
    progress_dialog.title("TCC Processing")

    bar = Progressbar(progress_dialog, orient="horizontal", length=500, value=0, mode="determinate")
    bar.grid(row=4, columnspan=2)
    t = threading.Thread(target=create_tcc, args=(bar, progress_dialog))
    t.start()
tcc_run_button = tk.Button(root, text="RUN", command=lambda:run_tcc())
tcc_run_button.pack()

root.mainloop()
Feanux
  • 33
  • 3
  • I think the program crashes because `tkinter` is single threaded and you are trying to put it into two threads. Anyway on python 3.x it does not crash for me – Delrius Euphoria Jun 23 '21 at 15:21