Progress bar while doing other processes

Viewed 499

I have made a python program based on Tkinter that analyses a CSV file and returns a PDF file. If there is a big CSV file, the tkinter window will freeze until the process is done. This is not desirable and instead I would like to introduce an indeterminate progress bar that disappears when a question is prompted for the user and then reappears when more processing is followed. I have read that somehow it needs a different thread, but I am quite stuck.

My program looks like:

import threading
from time import sleep
import tkinter as tk
import tkinter.ttk as ttk


root = tk.Tk()
root.title('text')
label = tk.Label(text = 'text')
label.pack()


class myThread(threading.Thread):
    def __init__(self, threadID):
        threading.Thread.__init__(self)
        self.threadID = threadID
    def run(self):
        func()


def func():
    #some processing
    #ask the user a question and based on the answer more processing will be done
    if True:
        #more processing1
        sleep(1)
    else:
        #more processing2
        sleep(1)

def check_thread(th):
    if not th.isAlive():
        root.destroy()
    root2.after(100, check_thread, th)


root2 = tk.Tk()
root2.title("New Top Level")
root2.geometry("400x170")

tk.Label(root2, text='Doing some work', justify=tk.CENTER, bg="#CBFDCB").place(x=43, y=30)
progress_bar = ttk.Progressbar(root2, orient="horizontal",
mode="indeterminate", takefocus=True, length=320)
progress_bar.place(x=40, y=80)
progress_bar.start()

thread1 = myThread(1)
thread1.start()

root2.after(100, check_thread, thread1)
root2.mainloop()

Button1 = tk.Button(root, text="sample", padx=10,
                    pady=5, fg="white", bg="#263D42", command=func)
Button1.pack()

root.mainloop()

Edit1: I have updated it with the version of the progress bar I was trying to use.

0 Answers
Related