Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
The Progressbar
widget in Tkinter, provided by the ttk
module, is used to provide a visual indication of the progress of a lengthy operation. It can operate in two modes: determinate mode shows the amount completed relative to the total amount of the operation, while indeterminate mode provides an animated display to show that work is ongoing.
This tutorial will guide you through using the Progressbar
widget in both modes.
import tkinter as tk from tkinter import ttk import time
root = tk.Tk() root.title("Progressbar Example") root.geometry("300x150")
Let's start with a determinate mode progress bar:
progress_var = tk.DoubleVar() progressbar_determinate = ttk.Progressbar( root, variable=progress_var, orient="horizontal", length=200, mode="determinate" ) progressbar_determinate.pack(pady=20)
You can update the progress by setting the value of progress_var
between 0 and 100 (considering the progress bar's maximum value is 100).
The indeterminate mode simply shows an animation indicating ongoing work:
progressbar_indeterminate = ttk.Progressbar( root, orient="horizontal", length=200, mode="indeterminate" ) progressbar_indeterminate.pack(pady=20)
To start and stop the animation, use the start()
and stop()
methods respectively.
For demonstration purposes:
def start_progress(): # Indeterminate Mode progressbar_indeterminate.start(10) time.sleep(2) progressbar_indeterminate.stop() # Determinate Mode for i in range(101): time.sleep(0.05) progress_var.set(i) root.update_idletasks() start_button = tk.Button(root, text="Start Progress", command=start_progress) start_button.pack(pady=10)
Here's the full code to demonstrate both progress bar modes in Tkinter:
import tkinter as tk from tkinter import ttk import time def start_progress(): progressbar_indeterminate.start(10) time.sleep(2) progressbar_indeterminate.stop() for i in range(101): time.sleep(0.05) progress_var.set(i) root.update_idletasks() root = tk.Tk() root.title("Progressbar Example") root.geometry("300x150") progress_var = tk.DoubleVar() progressbar_determinate = ttk.Progressbar( root, variable=progress_var, orient="horizontal", length=200, mode="determinate" ) progressbar_determinate.pack(pady=20) progressbar_indeterminate = ttk.Progressbar( root, orient="horizontal", length=200, mode="indeterminate" ) progressbar_indeterminate.pack(pady=20) start_button = tk.Button(root, text="Start Progress", command=start_progress) start_button.pack(pady=10) root.mainloop()
When you run this code, you'll see a window with two progress bars and a button. Clicking the button will animate the indeterminate progress bar first and then fill up the determinate progress bar. Adjust the sleep times as needed for your system.
Python Tkinter ProgressBar widget example:
import tkinter as tk from tkinter import ttk def start_progress(): progress_bar.start() def stop_progress(): progress_bar.stop() root = tk.Tk() root.title("ProgressBar Example") progress_bar = ttk.Progressbar(root, mode="indeterminate") progress_bar.pack(pady=20) start_button = tk.Button(root, text="Start Progress", command=start_progress) start_button.pack(side="left", padx=10) stop_button = tk.Button(root, text="Stop Progress", command=stop_progress) stop_button.pack(side="right", padx=10) root.mainloop()
Customizing Progressbar in Tkinter:
import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Custom ProgressBar Example") style = ttk.Style() style.configure("TProgressbar", thickness=50, # Set the thickness of the progress bar troughcolor="lightgray", # Set the color of the trough troughrelief="sunken" # Set the relief of the trough ) progress_bar = ttk.Progressbar(root, mode="indeterminate", style="TProgressbar") progress_bar.pack(pady=20) root.mainloop()
Tkinter ProgressBar update value:
import tkinter as tk from tkinter import ttk def update_progress(): current_value = progress_bar["value"] new_value = current_value + 10 progress_bar["value"] = new_value root = tk.Tk() root.title("Update ProgressBar Example") progress_bar = ttk.Progressbar(root, mode="determinate", maximum=100) progress_bar.pack(pady=20) update_button = tk.Button(root, text="Update Progress", command=update_progress) update_button.pack() root.mainloop()
Progress bar with percentage in Tkinter:
import tkinter as tk from tkinter import ttk def update_progress(): current_value = progress_bar["value"] new_value = current_value + 10 progress_bar["value"] = new_value percentage_label["text"] = f"{int(new_value)}%" root = tk.Tk() root.title("Progressbar with Percentage Example") progress_bar = ttk.Progressbar(root, mode="determinate", maximum=100) progress_bar.pack(pady=20) update_button = tk.Button(root, text="Update Progress", command=update_progress) update_button.pack() percentage_label = tk.Label(root, text="0%") percentage_label.pack() root.mainloop()