A Progress bar with status in percentage


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:

  1. The function takes five arguments:
    • master: the parent window or frame of the progress bar
    • x_loc: the x-coordinate of the top-left corner of the progress bar
    • y_loc: the y-coordinate of the top-left corner of the progress bar
    • bar_length: the length of the progress bar in pixels
    • total_lines: the total number of lines to be processed, which is used to calculate the progress percentage
  2. The global keyword is used to declare the CurrentLine variable as a global variable so that it can be used and modified within the function.
  3. The function checks if the CurrentLine variable is defined. If it is not defined, it is set to zero.
  4. The function tries to remove the progress label if it already exists, and creates a new one with the initial text “0%”.
  5. The function creates a ttk.Progressbar widget with a horizontal orientation, a specified length, and a “determinate” mode, which means the progress bar fills up gradually as progress is made.
  6. The place method is used to position the progress bar at the specified x_loc and y_loc coordinates.
  7. 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.floor function.
  8. The progress label is positioned to the right of the progress bar.
  9. The progress label text is updated to display the progress percentage followed by a “%” symbol.
  10. The master.update() method is called to update the GUI with the progress bar and label.
  11. The progress value of the progress bar is set to the progress percentage.

Leave a Reply

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