Tkinter Progressbar Animation
If you want to design an animated Progressbar, you can call the update( ) method every time you update the value of the Progressbar object, and then the window can be redrawn according to the value, so that the animation effect can be achieved.
Example#1
Design the Progressbar with drawing effect, the maximum value is 100, starting from 0, and it can move one frame every 0.05s.
from tkinter import *
from tkinter.ttk import *
import time
def running(): # Start Progressbar animation
for i in range(100):
pb["value"] = i + 1 # 1 per update
root.update() # Update screen
time.sleep(0.05)
root = Tk()
# root.geometry("300x140")
root.title("apidemos.com")
# Create a progress bar using the default settings
pb = Progressbar(root,length=200,mode="determinate",orient=HORIZONTAL)
pb.pack(padx=10,pady=20)
pb["maximum"] = 100
pb["value"] = 0
btn = Button(root,text="Running",command=running)
btn.pack(pady=10)
root.mainloop()
Output:
Suppose we are designing a Progressbar for downloading data. In a real application, we can use the obtained file size as the value of the maximum parameter of the Progressbar object, and then set the number of data to be read each time (the amount of downloads), and continue downloading as long as the total amount of downloads is less than the maximum. The following is the design of the Progressbar for simulated downloads.
Example#2
The Progressbar design for simulated downloads assumes that the total number of downloads is 10 000B and the number of data reads (downloads) per read is 500B.
from tkinter import *
from tkinter.ttk import *
def load(): # Start Progressbar
# global bytes
# bytes = 0
pb["value"] = 0 # Progressbar initial value
pb["maximum"] = maxbytes # Progressbar Max Value
loading()
def loading(): # Simulation download data
global bytes
bytes += 500 # Simulate 500B per download
pb["value"] = bytes # Set Pointer
if bytes < maxbytes:
pb.after(50,loading) # Continue loading after 0.05 seconds
root = Tk()
root.title("apidemos.com")
bytes = 0 # Set initial value
maxbytes = 10000 # Assuming the download file size
pb = Progressbar(root,length=200,mode="determinate",orient=HORIZONTAL)
pb.pack(padx=10,pady=10)
pb["value"] = 0 # Progressbar initial value
btn = Button(root,text="Load",command=load)
btn.pack(pady=10)
root.mainloop()
Output: