How to Create a Progress Bar in Python Tkinter?

Someone asked me how to create a progress bar using Python Tkinter. After research and experiment, I found several ways of representing the progress. In this tutorial, I will explain how to create a progress bar with examples and screenshots.

Progressbar Widget in Python Tkinter

The Progressbar widget in Tkinter is used to reassure the user that something is happening in the background. It can operate in two modes:

  1. Determinate mode: The progress bar shows the percentage of completion of a task.
  2. Indeterminate mode: The progress bar shows an animated effect to indicate that a process is ongoing, but the completion percentage is unknown.

The Progressbar widget is part of the ttk (themed Tk) module, which provides a set of themed widgets that look more modern compared to the classic Tkinter widgets.

Read How to Master the Python Tkinter Canvas?

Create a Progress Bar in Python Tkinter

To create a basic progress bar in Tkinter, follow these steps:

  1. Import the necessary modules:
import tkinter as tk
from tkinter import ttk
  1. Create a Tkinter window:
window = tk.Tk()
window.title("Progress Bar Example")
  1. Create a progress bar widget:
progress_bar = ttk.Progressbar(window, length=200, mode='determinate')
progress_bar.pack()
  1. Start the Tkinter event loop:
window.mainloop()

You can see the output in the screenshot below.

Create a Progress Bar in Python Tkinter

In this example, we create a progress bar with a length of 200 pixels and set its mode to ‘determinate’. The pack() method is used to add the progress bar to the window.

Check out How to Set and Manage Window Size in Python Tkinter?

Update the Progress Bar

To update the progress bar, you need to change its value attribute. Here’s an example that demonstrates how to update the progress bar based on a percentage:

import tkinter as tk
from tkinter import ttk

def update_progress_bar():
    progress_bar['value'] += 10
    if progress_bar['value'] < 100:
        window.after(1000, update_progress_bar)

window = tk.Tk()
window.title("Progress Bar Update Example")

progress_bar = ttk.Progressbar(window, length=200, mode='determinate')
progress_bar.pack()

update_progress_bar()

window.mainloop()

You can see the output in the screenshot below.

How to Create a Progress Bar in Python Tkinter

In this example, the update_progress_bar() function increments the progress bar’s value by 10 every second using the after() method. The function checks if the value is less than 100 before scheduling the next update.

Read How to Use the Tkinter Treeview Widget in Python?

Progress Bar with Start and Stop Buttons

Here’s an example that demonstrates how to create a progress bar with start and stop buttons:

import tkinter as tk
from tkinter import ttk

def start_progress():
    if not progress_bar['value']:
        progress_bar.start()
        start_button['state'] = 'disabled'
        stop_button['state'] = 'normal'

def stop_progress():
    progress_bar.stop()
    progress_bar['value'] = 0
    start_button['state'] = 'normal'
    stop_button['state'] = 'disabled'

window = tk.Tk()
window.title("Progress Bar with Buttons")

progress_bar = ttk.Progressbar(window, length=200, mode='indeterminate')
progress_bar.pack()

button_frame = ttk.Frame(window)
button_frame.pack()

start_button = ttk.Button(button_frame, text="Start", command=start_progress)
start_button.pack(side='left')

stop_button = ttk.Button(button_frame, text="Stop", command=stop_progress, state='disabled')
stop_button.pack(side='left')

window.mainloop()

You can see the output in the screenshot below.

Create a Progress Bar in Python Tkinter update

In this example:

  • The start_progress() function starts the progress bar and disables the start button while enabling the stop button.
  • The stop_progress() function stops the progress bar, resets its value, enables the start button, and disables the stop button.
  • The progress bar is set to ‘indeterminate’ mode, which shows an animated effect.

Check out How to Take User Input and Store It in a Variable Using Python Tkinter?

Customize the Progress Bar

You can customize the appearance of the progress bar using various options. Here are some common customization options:

OptionDescription
lengthThe length of the progress bar in pixels
modeThe mode of the progress bar (‘determinate’ or ‘indeterminate’)
maximumThe maximum value of the progress bar (default is 100)
valueThe current value of the progress bar
orientThe orientation of the progress bar (‘horizontal’ or ‘vertical’)

For example, to create a vertical progress bar with a maximum value of 50, you can use:

progress_bar = ttk.Progressbar(window, orient='vertical', length=100, maximum=50, mode='determinate')

Read How to Read a Text File and Display the Contents in a Tkinter With Python?

Conclusion

In this tutorial, I have explained how to create a progress bar using Python Tkinter. I discussed creating a progress bar, updating a progress bar, and progress bar with start and stop buttons, and customizing the progress bar.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.