Create a Progressbar in Tkinter Python

A progress bar is a graphical control element that used to visualize the progression of an extended computer operation, for example in the time of download, file transfer, or installation. In order to use progress bar in tkinter we first have to import tkinter.ttk module as progressbar is one of the six widgets that are not directly available in Tkinter.

The tkinter.ttk module provides access to the Tk themed widget set. To learn more about tkinter.ttk click here.

Now create a screen and use progress bar widget provided by tkinter.ttk to create a simple Progress bar on the screen.

from tkinter import *
from tkinter.ttk import Progressbar
from tkinter import ttk
scr=Tk()
scr.title("Progressbar")
scr.geometry('5000x1000')
s = ttk.Style()
s.theme_use('default')
s.configure("black.Horizontal.TProgressbar", background='red')
bar = Progressbar(scr, length=400, s='black.Horizontal.TProgressbar')
bar['value'] = 50
bar.place(x=500,y=400)

The above code will create a simple progress bar on the screen as shown in the figure below. The current bar value is 50 as specified using value attribute.

50% progress bar

However, this will just create a static progress bar on the screen with no functionality.

Functional Progressbar

Progress bars can be used in many different ways. They are often used to assure user that something is happening on the screen. One of the following example to use progress bar in tkinter is given below.

from tkinter import *
from tkinter.ttk import Progressbar
from tkinter import ttk
scr=Tk()
scr.title("Progressbar")
scr.geometry('5000x1000')
s = ttk.Style()
s.theme_use('default')
s.configure("black.Horizontal.TProgressbar", background='red')
b=Button(scr,text="Increase",fg="black",bg="green",font=('Time','13','bold'),command=lambda:increase())
b.place(x=550,y=450)
b=Button(scr,text="Decrease",fg="black",bg="red",font=('Time','13','bold'),command=lambda:decrease())
b.place(x=750,y=450)
bar = Progressbar(scr, length=400, s='black.Horizontal.TProgressbar')
bar['value'] = 20
bar.place(x=500,y=400)

def increase():
    if bar['value']<100:
        bar['value']=bar['value']+10
    if bar['value']<=30:
       s.configure("black.Horizontal.TProgressbar", background='red')
    elif bar['value']>30 and bar['value']<=60:
       s.configure("black.Horizontal.TProgressbar", background='orange')
    elif bar['value']>60 and bar['value']<=80:
       s.configure("black.Horizontal.TProgressbar", background='yellow')
    elif bar['value']>80:
       s.configure("black.Horizontal.TProgressbar", background='green')

def decrease():
    if bar['value']>0:
        bar['value']=bar['value']-10
    if bar['value']<=30:
        s.configure("black.Horizontal.TProgressbar", background='red')
    elif bar['value']>30 and bar['value']<=60:
       s.configure("black.Horizontal.TProgressbar", background='orange')
    elif bar['value']>60 and bar['value']<=80:
       s.configure("black.Horizontal.TProgressbar", background='yellow')
    elif bar['value']>80:
       s.configure("black.Horizontal.TProgressbar", background='green')


The given code block will create a progress bar on the screen along with two buttons. These buttons can modify the value of the progress bar.

Given below are different images taken at different instances when the above code is run. (Increase and Decrease buttons are used to modify the value of the progress bar)

Progressbar

Progressbar

Progress bar

Progressbar

This is how you can use other widgets to add functionality in a progress bar.

Leave a Reply

Your email address will not be published. Required fields are marked *