Home > Software engineering >  Threading with Tkinter to introduce Progress bar
Threading with Tkinter to introduce Progress bar

Time:09-27

I would like to introduce 'Indeterminate progress bar' for each of these two functions. Is there anyway to achieve it using Threading or any other method?

import threading
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import time


def submit_button1():
    i=0
    for i in range(5):
        time.sleep(2) 
        i =1
    messagebox.showinfo("", "Completed")

def submit_button2():
    j=0
    for j in range(5):
        time.sleep(3) 
        j =1
    messagebox.showinfo("", "Completed")
    

root = Tk()
root.geometry("600x300")
root.title("Progress Bar")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
ttk.Button(mainframe, text="submit_button1", command=submit_button1).grid(column=3, row=12, sticky=W)
    
ttk.Button(mainframe, text="submit_button2", command=submit_button1).grid(column=3, row=15, sticky=W)
    
root.mainloop()

CodePudding user response:

Your idea was already right. Just open a thread via an extra function.

import threading
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import time

class pseudo_example():
    def __init__(self):
        self.root = Tk()
        self.root.geometry("600x300")
        self.root.title("Progress Bar")

    def submit_button1(self):
        self.bu1["state"] = "disable"
        i = 0
        for i in range(101):
            time.sleep(.1)
            self.progressbar_1["value"] = i
            i  = 1
        messagebox.showinfo("", "Completed")
        self.bu1["state"] = "normal"
        self.progressbar_1["value"] = 0


    def submit_button2(self):
        self.bu2["state"] = "disable"
        i = 0
        for i in range(101):
            time.sleep(.1)
            self.progressbar_2["value"] = i
            i  = 1
        messagebox.showinfo("", "Completed")
        self.bu2["state"] = "normal"
        self.progressbar_2["value"] = 0

    def thread_sub1(self):
        threading.Thread(target=self.submit_button1, daemon=True).start()

    def thread_sub2(self):
        threading.Thread(target=self.submit_button2, daemon=True).start()

    def display(self):
        self.bu1 = ttk.Button(self.root, text="submit_button1", command=self.thread_sub1)
        self.bu1.grid(column = 0, row = 0)

        self.bu2 = ttk.Button(self.root, text="submit_button2", command=self.thread_sub2)
        self.bu2.grid(column = 0, row = 1)

        style = ttk.Style()
        style.configure('text.Horizontal.TProgressbar', anchor='center', troughcolor='white',
                        background='#009999')
        self.progressbar_1 = ttk.Progressbar(self.root, style='text.Horizontal.TProgressbar')
        self.progressbar_1.grid(column = 1, row = 0)

        self.progressbar_2 = ttk.Progressbar(self.root, style='text.Horizontal.TProgressbar')
        self.progressbar_2.grid(column=1, row=1)


        self.root.mainloop()


start = pseudo_example()
start.display()
  • Related