Python Tkinter Progress bar – How to use

In this Python tutorial, we will learn about the Python Tkinter progress bar. Also, we will cover these topics.

  • About Python Tkinter Progress bar
  • Python Tkinter Progress bar using determinate
  • Python Tkinter Progress bar using indeterminate
  • How to find the value of the progress bar
  • Applying text in the Progress bar
  • How to increase or decrease the width of the Progress bar
  • Using thread in Python Tkinter progress bar
  • How to update progress bar
  • Progress bar not updating error

Python Tkinter Progress bar

  • Python Tkinter progress bar tells the current status of the activity or task.
  • The progress bar makes users feel that something is happening in response to their click and they need to wait.
  • The progress bar has two modes:
    • determinate
    • indeterminate
python tkinter progress bar
python tkinter progress bar

Python Tkinter Progress bar determinate

  • Python Tkinter Progress bar determinate has a determined value that means the program knows the endpoint.
  • Determinate is a type of animation in which a box keeps on filling with color as the task keeps on completing.

Code:

In this code, determinate mode of progress bar is displayed

from tkinter import *
from tkinter.ttk import *
import time

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x250+1000+300')

def step():
    for i in range(5):
        ws.update_idletasks()
        pb1['value'] += 20
        
        time.sleep(1)

pb1 = Progressbar(ws, orient=HORIZONTAL, length=100, mode='indeterminate')
pb1.pack(expand=True)

Button(ws, text='Start', command=step).pack()

ws.mainloop()

Output:

python tkinter progress bar determinate
python tkinter progress bar determinate

Python Tkinter Progress bar indeterminate

  • Python Tkinter Progress bar indeterminate has an undetermined value that means the system doesn’t have a fixed end value.
  • Indeterminate is a type of animation in which a fixed size of the bar keeps on moving.
  • This mode of progress bar can be seen on the ‘error screen’ or ‘troubleshooting screen’.
python tkinter progress bar indeterminate example
Python Tkinter Progress bar indeterminate

Code:

from tkinter import *
from tkinter.ttk import *
import time

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x250+1000+300')

def step():
    for i in range(5):
        ws.update_idletasks()
        pb1['value'] += 20
        
        time.sleep(1)

pb1 = Progressbar(ws, orient=HORIZONTAL, length=100, mode='indeterminate')
pb1.pack(expand=True)

Button(ws, text='Start', command=step).pack()

ws.mainloop()

Output:

python tkinter progress bar indeterminate mode
Python Tkinter Progress bar indeterminate

Python Tkinter Progress bar value

  • Python Tkinter progress bar value determines the amount of step taken by the progress bar every time.
  • Let’s say the maximum length of the progress bar is 100. Then, each step to reach 100 is considered as a value.
  • If the value is 20 that means the progress bar will move 5 times to become 100.

Code:

In this code, we have provided the value as 20. And Since it is

from tkinter import *
from tkinter.ttk import *
import time

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x250+1000+300')

def step():
    for i in range(5):
        ws.update_idletasks()
        pb1['value'] += 20
        
        time.sleep(1)

pb1 = Progressbar(ws, orient=HORIZONTAL, length=100, mode='indeterminate')
pb1.pack(expand=True)

Button(ws, text='Start', command=step).pack()

ws.mainloop()

Output:

In this output, the sequence of progress is displayed & just below that the value of the progress bar is printed. You can observe that there is a difference of 20 in each value

Python Tkinter Progress bar text

  • In this section, we will learn how to implement a text on the Python progress bar.
  • The progress bar doesn’t have a feature to display text on its own.
  • We can do this using a Label widget. All we have to do is to place a Label widget where ever we want to implement the text.
  • Now we can configure the text value of the label & can change it with the change in the Python Tkinter progress bar.
  • In this section, we have placed a label on the right side of the progress bar. The label is provided the value of the progress bar.

Code:

In this code, under step function we have declared a loop that will run 5 times. this loop will keep on updating the progress bar value with 20.

Every time loop will run, it will update the value of the Label. ws.update_idletasks() is used so that we can see the progress. Otherwise, it will quickly happen. there is a gap of 1 second every time the loop is run.

from tkinter import *
from tkinter.ttk import Progressbar
import time


def step():
    for i in range(5):
        ws.update_idletasks()
        pb['value'] += 20
        time.sleep(1)
        txt['text']=pb['value'],'%'

ws = Tk()
ws.title('PythonGuides')
ws.geometry('200x150')
ws.config(bg='#345')


pb = Progressbar(
    ws,
    orient = HORIZONTAL,
    length = 100,
    mode = 'determinate'
    )

pb.place(x=40, y=20)

txt = Label(
    ws,
    text = '0%',
    bg = '#345',
    fg = '#fff'

)

txt.place(x=150 ,y=20 )

Button(
    ws,
    text='Start',
    command=step
).place(x=40, y=50)

ws.mainloop()

Output:

In this output, text next to Progress bar displays the value of progress bar.

python tkinter text on progress bar
Python tkinter text on progress bar

Python Tkinter Progress bar width

  • If you are wondering, how to increase the width of the progress bar in Tkinter.
  • Then the simplest solution is to increase the length value. The length value determines the width of the progress bar
  • Here we will project two examples with different widths.

Code:

in this code, we have created three different progress bars that are running using the inbuilt function start(). Two of the progress bars are determinate & one in the middle is indeterminate.

from tkinter import *
from tkinter.ttk import Progressbar
import time


ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x350')
ws.config(bg='#345')


pb1 = Progressbar(
    ws,
    orient = HORIZONTAL,
    length = 50,
    mode = 'determinate'
    )

pb1.place(x=40, y=20)

pb2 = Progressbar(
    ws,
    orient = HORIZONTAL,
    length = 100,
    mode = 'indeterminate'
    )

pb2.place(x=40, y=80)

pb3 = Progressbar(
    ws,
    orient = HORIZONTAL,
    length = 150,
    mode = 'determinate'
    )

pb3.place(x=40, y=140)

pb1.start()
pb2.start()
pb3.start()

ws.mainloop()

Output:

In this output, You can see that all the three progress bars have different width.

python tkinter progress bar width
Python tkinter progress bar width

Python Tkinter Progress bar thread

  • Python runs the program line-by-line. So, the second part of the program runs only after the first part is complete.
  • In this case, a dependency is created which can delay the program.
  • To resolve this issue & to make programs independent concept of threading is introduced.
  • In threading, every part of the program runs as a separate unit.

Code:

In this code, we have created program using thread. Thread helps in avoiding screen freezing. As it enables the part of program run independently.

Here the progress bar will start after 5 seconds. In the meantime the screen it freezes and the user can’t do anything. But with thread, it is possible to click on other buttons or close the window.

from tkinter import *
from tkinter.ttk import Progressbar
import time
import threading


ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
ws.config(bg='#345')

def start_download():
    time.sleep(5)
    pb.start()   
    
def stop_download():
    pb.stop()

pb = Progressbar(
    ws,
    orient = HORIZONTAL,
    length = 200,
    mode = 'determinate'
    )
pb.pack()

msg = Label(
    ws,
    text='',
    bg='#345',
    fg='red',
    
)

msg.pack()

start = Button(
    ws,
    text='Start Download',
    command=threading.Thread(target=start_download).start()
    #command=start_download
    )

start.pack()

stop = Button(
    ws,
    text='Stop Download',
    command=stop_download
)
stop.pack()

ws.mainloop()from tkinter import *
from tkinter.ttk import Progressbar
import time
import threading


ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x350')
ws.config(bg='#345')


def play():
    time.sleep(10)
    pb.start()

pb = Progressbar(
    ws,
    orient = HORIZONTAL,
    length = 100,
    mode = 'determinate'
    )
pb.pack(pady=30)

play = Button(
    ws,
    text="Freeze",
    #command=threading.Thread(target=play).start())
    command=play)
play.pack(pady=30)

pb1 = Progressbar(
    ws,
    orient = HORIZONTAL,
    length = 100,
    mode = 'determinate'
    )

pb1.start()
pb1.pack(pady=30)

ws.mainloop()

Output:

In this output, the image in fig 1.1 works perfectly and does not freeze the screen when clicked on the start download button. Whereas, an image in fig 1.2 shows that screen freezes for 5 seconds when clicked on the start download button. So that is the benefit of threading.

python tkinter progress bar using threading
python tkinter progress bar using threading

Python Tkinter Progress bar update

  • Progress bar in python Tkinter shows some update every time an event occurs.
  • It makes users feel that something is happening and they need to wait. There is a line of code that you might have observed that says update_idletasks().
  • update_idletasks() allows us to see the increment in the progress bar.
  • With this line of code, the human eye will see 100|% completion of the progress bar only.
  • To see an example, please refer to our Python Tkinter Progress bar Text Section.

Python Tkinter Progress bar not updating

  • The Python Tkinter progress bar is supposed to keep on updating if the progress bar is not updating it intimates the user that nothing is happening.
  • Progress bars are bound to the time that instructs the user to hold for a moment.
  • There are various possible reasons for the progress bar not updating.
  • Here are a few tips that you can follow:
    • The value of increment should always be less than the length of the progress bar
    • Try providing increment value as the perfect divisible of length. for example, if the length is 100 and 20 or 10 will be perfect as increment value as a reminder will be 0 in the end.
    • While using a loop, make sure to provide

You may like the following Python tutorials:

In this tutorial, we have learned to implement a progress bar using Python Tkinter. Also, we have covered these topics.

  • About Python Tkinter Progress bar
  • Python Tkinter Progress bar using determinate
  • Python Tkinter Progress bar using indeterminate
  • How to find the value of the progress bar
  • Applying text in the Progress bar
  • How to increase or decrease the width of the Progress bar
  • Using thread in Python Tkinter
  • How to update progress bar
  • Progress bar not updating error