SoFunction
Updated on 2024-10-30

Python tkinter Progressbar Progressbar Usage Explanation

Progressbar Basic Concepts

Progressbar can be interpreted asprogress barThe main purpose of this control is to serve as a pointer to the progress of the work, and there will be a pointer in this control from which you can understand the progress of the work.

The construction method is as follows:

Progressbar(parent object (computing), options, ...)

Parameters:

  • The first parameter:parent object (computing)This indicates the window in which the progress bar will be built.
  • Second parameter:optionsThe parameters are as follows
parameters hidden meaning
length The default length of the progress bar is 100 pixels
mode There can be two modes, which are described below
maximum The maximum value of the progress bar defaults to 100 pixels
name Name of the progress bar for program reference citation
orient The direction of the progress bar, either HORIZONTAL (default) or VERTICAL.
value Current value of the progress bar
variable Record the current progress value of the progress bar

mode parameter

  • determinate: apointer on a gaugeIt will move from the start point to the end point, usually this mode can be used when we know the required working time, this is the default mode
  • indeterminate: apointer on a gaugewill move back and forth between the start and end points, usually when we don't know how long the job will take Progressbar

basic application

Example:

import tkinter
import 
root = ()
('150x120')
progressbarOne = (root)
(pady=20)
# Maximum progress value
progressbarOne['maximum'] = 100
# Initial value of progress value
progressbarOne['value'] = 20
# Horizontal rows Length 200 Starting point moved to end point
progressbarTwo = (root, orient=, length=200, mode='determinate')
(pady=20)
# Maximum progress value
progressbarTwo['maximum'] = 100
# Initial value of progress value
progressbarTwo['value'] = 80
()

Run results:

Note: The progress bar can't move yet!

Progressbar Animation Design

If you want to design a Progressbar with an animated effect, you can call theupdate()method, which allows the window to be used as a window based on the value of therepaintThis is how the animation effect is achieved

Example:

import time
import tkinter
import 
def show():
    for i in range(100):
        # Plus one for each update
        progressbarOne['value'] = i + 1
        # Update the screen
        ()
        (0.05)
root = ()
('150x120')
progressbarOne = (root)
(pady=20)
# Maximum progress value
progressbarOne['maximum'] = 100
# Initial value of progress value
progressbarOne['value'] = 0
button = (root, text='Running', command=show)
(pady=5)
()

Run results:

Example:

To simulate real downloads, the total amount of downloads is 10,000B, 500B per download.

def show():
    # Set the current value of the progress bar
    progressbarOne['value'] = 0
    # Set the maximum value of the progress bar
    progressbarOne['maximum'] = maxbyte
    # Call the loading method
    loading()
def loading():
    # Change variable properties
    global byte
    # 500B per run
    byte += 500
    # Setting pointers
    progressbarOne['value'] = byte
    if byte < maxbyte:
        # Call the loading method again after 100ms
        (100, loading)
root = ()
('150x120')
# Set initial download values
byte = 0
# Set the download maximum
maxbyte = 10000
progressbarOne = (root)
(pady=20)
button = (root, text='Running', command=show)
(pady=5)
()

Run results:

Progressbar methods start()/step()/stop()

The meaning is as follows:

  • start(interval)The default value of interval is 50ms, and step(amount) is called every time the pointer is moved. The significance of the amount parameter in the step() method is the amount of value added.
  • step(amount): Increase the AMOUNT once each time, the default value is 1.0, in determinate mode, the pointer will not exceed the MAXIMUM parameter. In indeterminate mode, when the pointer reaches the previous frame of the value of the maximum parameter, the pointer will return to the starting point
  • stop(): stop start() from running

Example:

import time
import tkinter
import 
def show():
    while ('value') <= progressbarOne['maximum']:
        (2)
        ()
        print(('value'))
        (0.05)
root = ()
('150x120')
progressbarOne = (root, length=200, mode='determinate', orient=)
(pady=20)
progressbarOne['maximum'] = 100
progressbarOne['value'] = 0
button = (root, text='Running', command=show)
(pady=5)
()

Run results:

Use the start() method to start the animation and click the stop button to stop it.

Example:

import tkinter
import 
def run():
    ()
def stop():
    ()
root = ()
('150x120')
progressbarOne = (root, length=200, mode='determinate', orient=)
(padx=5, pady=10)
progressbarOne['maximum'] = 100
progressbarOne['value'] = 0
buttonRun = (root, text='Run', width=6, command=run)
(padx=10, pady=5, side=)
buttonStop = (root, text='Stop', width=6, command=stop)
(padx=10, pady=5, side=)
()

Run results:

indeterminate mode

In this mode the pointer will move left and right, the main purpose is to let the user know that the program is still continuing to work

Example:

progressbarOne = (root, length=200, mode='indeterminate', orient=)

Run results:

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.