0

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.

  • I'm not a tkinter user but aren't you supposed to use `progressbar.step()` instead of `progress_var.set()` ? –  Apr 02 '19 at 08:53
  • Can you fix the indentation in you code first? Also `para1_range` is undefined. – acw1668 Apr 02 '19 at 09:04
  • Try this approach [How to create downloading progress bar in ttk?](https://stackoverflow.com/a/7310778/7414759) – stovfl Apr 02 '19 at 10:59

0 Answers0