0

I am trying to use Tkinter to sequentially create progress bars (from this post: tkinter-progressbar-with-indeterminate-duration), for multiple functions.

In the function _wrap_func_with_thread below, if using root.quit(), each new instance creates a blank Tk window, and the old progress bars stay. If using root.destroy(), then the program stops after having run the first function.

How can I have a fresh and clean start each time?

    import tkinter as tk
    from tkinter import ttk
    import time
    import threading
    
    
    def run_func_with_status_window(func, lArgs, sText):
        root = tk.Tk()
        t1 = threading.Thread(target=_wrap_func_with_thread, args=(func, lArgs, root))
        t1.start()
        run_progress_bar(root, sText)
        t1.join()
        print('done')
    
    
    def _wrap_func_with_thread(func, lArgs, root):
        func(*lArgs)
        root.quit()   
        # root.destroy()
    
    
    def run_progress_bar(root, sText):
        root.update_idletasks()  # Add this line
        width = root.winfo_width()
        height = root.winfo_height()
        x = (root.winfo_screenwidth() // 2) - (width // 2)
        y = (root.winfo_screenheight() // 2) - (height // 2)
        root.geometry('{}x{}+{}+{}'.format(width, height, x, y))
    
        label = ttk.Label(text=sText)
        label.pack()
    
        ft = ttk.Frame()
        ft.pack(expand=True, fill=tk.BOTH, side=tk.TOP)
    
        pb_hD = ttk.Progressbar(ft, orient='horizontal', mode='indeterminate')
        pb_hD.pack(expand=True, fill=tk.BOTH, side=tk.TOP)
        pb_hD.start(50)
    
        root.mainloop()
    
    
    def f(x, y, z):
        time.sleep(3)
        print('x: {}, y: {}, z:{}'.format(x, y, z))
    

    if __name__ == '__main__':
        for ls in [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]:
            print('x: {}, y: {}, z:{}'.format(*ls))
            run_func_with_status_window(f, ls, 'x: {}, y: {}, z:{}'.format(*ls))
newbie
  • 275
  • 1
  • 3
  • 15
  • You shouldn't call `tkinter` methods from threads other than the one where you created the `tk.Tk`. `tkinter` doesn't play nice with threads. – TheLizzard Sep 20 '22 at 21:22

0 Answers0