Tkinter Progressbar start/step/stop Method
The meanings of these methods are as follows.
(1) start(interval)
: Move the pointer once every interval time. the default value of interval is 50ms, and call step(delta) once for each pointer move. The meaning of the delta parameter within the step( ) method is the amount of value added.
(2) step(delta)
: increase delta once each time, the default value is 1.0, in determinate mode, the pointer will not exceed the maximum parameter value. In indeterminate mode, when the pointer reaches the previous frame of the maximum parameter value, the pointer will return to the starting point.
(3) stop()
: Stop the operation of start( ).
Example#1
The verification uses the step(2) method, which is equivalent to adding value 2 at a time, and when the pointer reaches one frame before the end value of 100 (equivalent to 98), the pointer goes back to 0 and then starts moving again. This program is executed while the current value of the pointer is listed in the Python Shell window.
from tkinter import *
from tkinter.ttk import *
import time
def running(): # Start Progressbar animation
while pb.cget("value") <= pb["maximum"]:
pb.step(2)
root.update() # Update screen
print(pb.cget("value")) # Print pointer values
time.sleep(0.05)
root = Tk()
root.title("apidemos.com")
pb = Progressbar(root,length=200,mode="determinate",orient=HORIZONTAL)
pb.pack(padx=10,pady=10)
pb["maximum"] = 100
pb["value"] = 0 # Progressbar initial value
btn = Button(root,text="Running",command=running)
btn.pack(pady=10)
root.mainloop()
Output:
Example#2
Use the start() method to start the animation of the Progressbar and click the Stop button to stop it.
from tkinter import *
from tkinter.ttk import *
def run(): # Start Progressbar animation
print("run() Executed!!!")
pb.start() # Pointer moves 1 at a time
def stop(): # Terminate Progressbar animation
print("stop() Executed!!!")
pb.stop() # Terminate pb object animation
root = Tk()
root.title("apidemos.com")
pb = Progressbar(root,length=200,mode="determinate",orient=HORIZONTAL)
pb.pack(padx=5,pady=10)
pb["maximum"] = 100
pb["value"] = 0
btnRun = Button(root,text="Run",command=run) # Create Run button
btnRun.pack(side=LEFT,padx=5,pady=10)
btnStop = Button(root,text="Stop",command=stop) # Create Stop button
btnStop.pack(side=LEFT,padx=5,pady=10)
root.mainloop()
Output: