Progressbar widget in Tkinter

Progressbar widget in Tkinter

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.

1. Importing Required Modules:

import tkinter as tk from tkinter import ttk import time 

2. Creating the Main Application Window:

root = tk.Tk() root.title("Progressbar Example") root.geometry("300x150") 

3. Determinate Mode Progressbar:

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).

4. Indeterminate Mode Progressbar:

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.

5. Update Function for Demonstrating the Progressbars:

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) 

6. Complete Example:

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.

Examples

  1. Python Tkinter ProgressBar widget example:

    • Description: Create a simple Tkinter window with a ProgressBar widget.
    • Code 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() 
  2. Customizing Progressbar in Tkinter:

    • Description: Showcase customizing the appearance of the Tkinter Progressbar widget.
    • Code Example:
      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() 
  3. Tkinter ProgressBar update value:

    • Description: Illustrate updating the value of the Tkinter ProgressBar widget.
    • Code Example:
      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() 
  4. Progress bar with percentage in Tkinter:

    • Description: Demonstrate a progress bar with a percentage label in Tkinter.
    • Code Example:
      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() 

More Tags

form-data font-size mpandroidchart struts2 location maven regression wkhtmltoimage coldfusion internal-server-error

More Programming Guides

Other Guides

More Programming Examples