I have initialized a progress bar and want to update it according to the function / thread which is outside the main class of Tkinter. I have tried all the solutions present over here similar to the problem but to no avail. Any help will be greatly appreciated. Here is my code:
from tkinter import ttk
import time
import tkinter as tk
def run():
global progressBar
progressBar['maximum'] = 100
for i in range(0,100,25):
time.sleep(0.05)
progressBar["value"] = i
progressBar.update()
progressBar["value"] = 0
progressBar["value"] = 100
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
buttonFrame = tk.Label(text="Progress Bar")
buttonFrame.grid(column=0,row=0)
progressBar = ttk.Progressbar(self, orient="horizontal", length=286,mode="determinate")
progressBar.grid(column = 0, row = 3, pady=10)
button1 = tk.Button(buttonFrame, text="Run Progress Bar" ,command = run)
button1.grid(column = 0, row = 0)
app = Main()
app.mainloop()
You have used OOP.You could make the most of the class.I have reconstitute your code(Also,there is an error in your code):
from tkinter import ttk
import time
import tkinter as tk
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.buttonFrame = tk.Label(text="Progress Bar")
self.buttonFrame.grid(column=0,row=0)
self.progressBar = ttk.Progressbar(self, orient="horizontal", length=286,mode="determinate")
self.progressBar.grid(column = 0, row = 3, pady=10)
# this shouldn't be run() or self.run().No "()" there(Or you can use lambda if you need to pass arguments).
self.button1 = tk.Button(self.buttonFrame, text="Run Progress Bar" ,command = self.run)
self.button1.grid(column = 0, row = 0)
def run(self):
self.progressBar['maximum'] = 100
for i in range(0, 100, 25):
time.sleep(0.05)
self.progressBar["value"] = i
self.progressBar.update()
self.progressBar["value"] = 0
self.progressBar["value"] = 100
app = Main()
app.mainloop()