简体   繁体   中英

TKinter Popup Progress Bar

I am attempting to make a popup progress bar that shows the progress of some files downloading after a button is clicked. I can execute the command linked with the button perfectly, but I am struggling to create a popup progress bar.

Here is what I have so far

 def button_command(self):
    #start progress bar
    popup = tk.Toplevel()
    tk.Label(popup, text="Files being downloaded").grid(row=0,column=0)

    progress = 0
    progress_var = tk.DoubleVar()
    progress_bar = ttk.Progressbar(popup, variable=progress_var, maximum=100)
    progress_bar.grid(row=1, column=0)#.pack(fill=tk.X, expand=1, side=tk.BOTTOM)
    popup.pack_slaves()

    progress_step = float(100.0/len(teams))
    for team in self.teams:

        self.do_work()


        progress += progress_step
        progress_var.set(progress)
        popup.update_idletasks()


    return 0

It's creating a popup window currently, but there is nothing in it. If anyone has experience with this, the help would be much appreciated!

Thanks, Tyler

I think your problem is due to the position of update_idletasks in your for loop. You call it after the first call to self.do_work , so the GUI with the progressbar is only updated/displayed after the task is completed. At least, that's what I experienced by testing your code with time.sleep instead of do_work . You should therefore start by updating the GUI before launching the first task. I also noticed that the progressbar was displayed sooner when I used update instead of update_idletasks , but I don't know why.

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

teams = range(100)

def button_command():
    #start progress bar
    popup = tk.Toplevel()
    tk.Label(popup, text="Files being downloaded").grid(row=0,column=0)

    progress = 0
    progress_var = tk.DoubleVar()
    progress_bar = ttk.Progressbar(popup, variable=progress_var, maximum=100)
    progress_bar.grid(row=1, column=0)#.pack(fill=tk.X, expand=1, side=tk.BOTTOM)
    popup.pack_slaves()

    progress_step = float(100.0/len(teams))
    for team in teams:
        popup.update()
        sleep(5) # lauch task
        progress += progress_step
        progress_var.set(progress)

    return 0

root = tk.Tk()

tk.Button(root, text="Launch", command=button_command).pack()

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM