Python

Tkinter Progress Bar

When working with GUIs, it is often a great option to showcase the amount of the process done in the form of a progress bar. Since the Tkinter is a GUI-building library of Python, it provides a built-in method to build a progress bar easily. This guide will show you how not only to build a progress bar but also how to use two different modes of it and change the value of the progress bar.

How to Build a Progress bar in Tkinter Python?

You need to import the “ttk” package and use its Progressbar() method.

progVar= ttk.Progressbar(frameVar, orient, length, mode)

 

In the syntax of the Progressbar() method:

  • progVar: Is the variable that will be used to access the different attributes of the progress bar after its creation
  • frameVar: Is the Tkinter Tk variable, which is the frame/pane on which the progress would be displayed
  • orient: This argument will be used to define and set the orientation of the progress bar
  • length: This argument will be used to define and length of the progress bar
  • mode: The mode argument is used to set the mode to either “determinate” or “indeterminate”

Let’s build a simple progress bar in Tkinter, which progresses by 10% upon the press of a button by using the following code:

#Start by importing the Tkinter libraries

from tkinter import *
from tkinter import ttk
#After the import, create the Frame by Using the TK() Method
frame = Tk()
frame.title("LinuxHint - Progress Bar")
frame.geometry("400x200")
window = Frame(frame)
#Method to Increase Progress Value
def pBar():
    progBar['value']+=10
#Build Progressbar
progBar = ttk.Progressbar(frame,orient=HORIZONTAL, length=400,mode="determinate")
progBar.pack(pady=40)
#Build Button
btn = Button(frame, text="Click Here!",command=pBar)
btn.pack(pady=10)
frame.mainloop()

 

In this code snippet:

  • First, import the “tkinter” package and also the “ttk” package, which contains the Progressbar() method
  • Create a frame by using the Tk() method
  • Define the method pBar(), which uses the progress bar variable “progBar” to access the “value” attribute and increases it by 10
  • Create the “progBar” variable by using the Progressbar() method, and set the orientation to Horizontal and mode to determinate
  • Lastly, create a button that calls the pBar() method to increase the value of the progress bar.

When this code snippet is executed, it produces the following Tkinter window on your machine:

As you can see in the output GIF, the progress bar was created, and it kept progressing upon the click of the button. However, once the progress bar has hit the maximum length, no further change occurs upon button press.

How to Make Progress Bar Go Back and Forth in Tkinter Python?

To make the progress bar go back and forth upon the press of the button, you need to use the “indeterminate” progress bar. With an “indeterminate” progress bar, the aim is not to show the percentage of work done by the process to the user. Rather the aim is only to showcase that an action/event is in process.

To create an indeterminate progress bar, simply change the mode argument in the Progressbar() method to “indeterminate”:

progBar = ttk.Progressbar(frame,orient=HORIZONTAL, length=400,mode="determinate")

 

Place this line in the previous example, execute the program, and It will produce the following Tkinter window:

As you can see in the output GIF, the indeterminate progress bar didn’t stop at the 100% mark, rather it started to go back and forth upon the button clicks.

How to Reset the Progress bar in Tkinter Python?

To reset the value of the progress bar, simply use the progress bar variable to access the “value” attribute, and set its value equal to zero. A good practice is to reset the value of the progress bar upon a button press.

To demonstrate this, take this modified code from the first example:

from tkinter import *
from tkinter import ttk
#After the import, create the Frame by Using the TK() Method
frame = Tk()
frame.title("LinuxHint - Progress Bar")
frame.geometry("400x200")
window = Frame(frame)
#Method to Increase/Reset Progress Value
def pBar():
    progBar['value']+=10
def resetPBar():
    progBar['value']=0
#Build Progressbar
progBar = ttk.Progressbar(frame,orient=HORIZONTAL, length=400,mode="indeterminate")
progBar.pack(pady=40)
#Build Buttons
btn = Button(frame, text="Progress",command=pBar)
btn.pack(pady=10)
btn2 = Button(frame, text="Reset",command=resetPBar).pack(padx=10)
frame.mainloop()

 

When this code is executed, it produces the following behavior of the Tkinter Window on your machine:

You can observe that as soon as the “Reset” button is pressed, the progress is reset to 0.

Conclusion

Progress bars are a great way of informing the end user about an action’s completion percentage or execution status. To build a Progress bar in Tkinter, use the Progressbar() method of the “ttk” package and pass the values for frame, orientation, length, and mode. To change the progress value of the progress bar, access its attribute “value” and change its value according to your requirement.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.