Header Ads

Header ADS

Progressbar in tkinter

Tkinter : Progressbar

A progress bar is a graphical control element used to visualize the progression of an extended computer operation, such as a download, file transfer, or installation. Sometimes, the graphic is accompanied by a textual representation of the progress in a percent format. 

In this article you will learn how to create a Progressbar and work with its different attributes .

So how a progress bar looks like ?
before coding :

The progressbar is categorised on the basis of two modes .

  • In determinate mode, the widget shows an indicator that moves from beginning to end under program control.
  • In indeterminate mode, the widget is animated so the user will believe that something is in progress. In this mode, the indicator bounces back and forth between the ends of the widget.

1. Detrerminate Progressbar :

# tkinter : Dynamic Coding
# Progressbar ....

# importing tkinter module 
from tkinter import * 
from tkinter.ttk import *

  
# creating tkinter window 
root = Tk() 
root.title('Dynamic Coding')
  
# Progress bar widget with Indeterminate mode
progress = Progressbar(root, orient = HORIZONTAL, 
              length = 200, mode = 'determinate') 

value = 0 # variable for upadting Progressbar value

# Function responsible for the updation 
# of the progress bar value 
def bar():  
    global value
    value += 10
    progress['value'] = value 
    # updates the window widgets 
    root.update_idletasks()
    # calls the function bar after each 200 mili seconds
    root.after(200,bar)
  
progress.pack(pady = 10) 
  
# creating button to call the function which 
# updates the progress bar values
Button(root, text = 'Start', command = bar).pack(pady = 10) 
  
# infinite loop 
root.mainloop()

Output :





2. Indetrerminate Progressbar :

# tkinter : Dynamic Coding
# Progressbar ....

# importing tkinter module 
from tkinter import * 
from tkinter.ttk import *

  
# creating tkinter window 
root = Tk() 
root.title('Dynamic Coding')

# Progress bar widget with Indeterminate mode
progress = Progressbar(root, orient = HORIZONTAL, 
              length = 200, mode = 'indeterminate') 
value = 0
# Function responsible for the updation 
# of the progress bar value 
def bar():  
    global value
    value += 10
    progress['value'] = value 
    # updates the window widgets 
    root.update_idletasks()
    # calls the function bar after each 200 mili seconds
    if value != 300:
        root.after(200,bar)
    else:
        print(" process complete")
  
progress.pack(pady = 10) 
  
# creating button to call the function which 
# updates the progress bar values
Button(root, text = 'Start', command = bar).pack(pady = 10) 
  
# infinite loop 
root.mainloop()

Output :







Progressbar Attributes :

1. length 
Sets the length for widget.

2. maximum 
Set the maximum possible -value. Default is 100.

3. mode
Mode can be indeterminate or determinate.

4. orient 
Sets the orientation for widget. It can be either horizontal or vertical.

5. value 
The current progress of the progress bar.

6. variable 
Variable associated with the widget. When the text of widget changes, the variable is set to text of widget.

No comments

Powered by Blogger.