A Progress bar with status in percentage
- tinyytopic.com
- 1
- on Feb 16, 2023
How to create a Progress bar with a status display in percentage?
Install the following module(s) if you haven’t installed them already:
pip install tkinter
Ready-to-use Python function to display the progress bar along with the status text in percentage:
def progress_bar_with_status(master, x_loc, y_loc, bar_length, total_lines):
global CurrentLine
# check if variable is defined
try: CurrentLine
except: CurrentLine = 0
# Remove if label exists
try:
txtProgress.config(text='')
except:
txtProgress = Label(master, text = '0%', bg = '#345', fg = '#fff')
txtProgress = Label(master, text = '0%', bg = '#345', fg = '#fff') #Progress Label
# progress bar with status
CurrentLine +=1
progress = ttk.Progressbar(master, orient = HORIZONTAL, length = bar_length, mode = 'determinate')
progress.pack(pady = 10)
progress.place(x=x_loc, y = y_loc)
progress['value'] = math.floor(CurrentLine * 100/total_lines)
txtProgress.place(x=bar_length+30, y = y_loc)
txtProgress['text'] = progress['value'],'%'
master.update()
Write your main code as a sample below,
root = Tk()
root.title("Progress")
root.geometry("605x550")
total_lines = 5
for i in range(0, total_lines):
time.sleep(1)
progress_bar_with_status(root, 20, 30, 150, total_lines)
root.mainloop()
The output of the code is,

How does the function work?
This is a Python function that creates a progress bar with a status label in a graphical user interface (GUI) using the tkinter and ttk libraries.
Here’s how the function works:
- The function takes five arguments:
master: the parent window or frame of the progress barx_loc: the x-coordinate of the top-left corner of the progress bary_loc: the y-coordinate of the top-left corner of the progress barbar_length: the length of the progress bar in pixelstotal_lines: the total number of lines to be processed, which is used to calculate the progress percentage
- The
globalkeyword is used to declare theCurrentLinevariable as a global variable so that it can be used and modified within the function. - The function checks if the
CurrentLinevariable is defined. If it is not defined, it is set to zero. - The function tries to remove the progress label if it already exists, and creates a new one with the initial text “0%”.
- The function creates a
ttk.Progressbarwidget with a horizontal orientation, a specified length, and a “determinate” mode, which means the progress bar fills up gradually as progress is made. - The
placemethod is used to position the progress bar at the specifiedx_locandy_loccoordinates. - The progress percentage is calculated by dividing the current line number by the total number of lines, multiplying by 100, and rounding down to the nearest integer using the
math.floorfunction. - The progress label is positioned to the right of the progress bar.
- The progress label text is updated to display the progress percentage followed by a “%” symbol.
- The
master.update()method is called to update the GUI with the progress bar and label. - The progress value of the progress bar is set to the progress percentage.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good. create binance account