I am trying to display the progressbar correctly by counting the iterations of my for loop. When the for loop starts the tkinter window appears with no progress bar and just hangs.
I've tried creating a "count = 0" and then the progress should peg of "count +=1" during each iteration.
import time
import tkinter as tk
from tkinter import ttk
para1_range = [0,1,2,3,4,5,6,7,8,9,10]
MAX = len(para1_range)
count = 0
for k in range(0,len(para1_range)):
time.sleep(1)
root = tk.Tk()
root.geometry('{}x{}'.format(400, 100))
progress_var = tk.DoubleVar() #here you have ints but when calc. %'s usually floats
theLabel = tk.Label(root, text="Sample text to show")
theLabel.pack()
progressbar = ttk.Progressbar(root, variable=progress_var, maximum=MAX, mode="determinate")
progressbar.pack(fill=tk.X, expand=1)
def loop_function():
while count <= MAX:
### some work to be done
progress_var.set(count)
root.update_idletasks()
root.after(100, loop_function)
count += 1
loop_function()
root.mainloop()
I expected a progress bar directly mapping the progress of the for loop progress. Instead I get a frozen window with no progress bar. Any ideas hat is wrong with this code? Much appreciated.