Progress Bar Widget in Tkinter (GUI Programming) – Python Tkinter Tutorial
Progress Bar widget Python provides a vast number of libraries for GUI application development. Tkinter is the widely used library for GUI application development. Using Tkinter library of python which carries large numbers of widgets, we can easily create a Graphical User Interface for our application.
Progress Bar: In Tkinter, Progress bar widget is used to display the user about that something is happening. Mostly this type of widget is used in loading or downloading types of process. The progress bar executes in two modes:-
- Indeterminate mode
- Determinate mode
Syntax:
z = ttk.Progressbar(root, options)
Parameters:
- root: It represents the parent window.
- options: Various options are used along with the progress bar widget. It will be more clear in examples.
Let’s have some examples to understand this better
Example 1: Example of a progress bar widget in “indeterminate mode” i.e. mode=’indeterminate’
from tkinter import * from tkinter import ttk root = Tk() label = Label(root, text ='Progress Bar', font = "50") label.pack(pady=5) #Progress Bar progbar = ttk.Progressbar(root, orient=HORIZONTAL, length=220, mode="indeterminate") progbar.pack(pady=20) progbar.start() root.geometry("300x150") root.title("PythonLobby.com") root.mainloop()
Output:
Explanation:
- First, we have imported all the required libraries and modules of Tkinter using import *.
- Then we have initialized root as an object for Tk() class for creating a root window.
- Here we have placed a label on our root window.
- Progress bar widget available in ttk library of Tkinter. Hence we have also imported ttk.
- Here we have used indeterminate mode for our progress bar.
- Parameters we used, orient as HORIZONTAL for the horizontal layout of our progress bar, mode as ‘indeterminate’ and length as 220.
- To start the progress bar we also need to call inbuilt function of progressbar i.e. progbar_obj.start().
Example 2: Example of a progress bar widget in “determinate mode” i.e. mode=’determinate’
from tkinter import * from tkinter import ttk root = Tk() label = Label(root, text ='Progress Bar', font = "50") label.pack(pady=5) #Progress Bar progbar = ttk.Progressbar(root, orient=HORIZONTAL, length=220, mode="determinate") progbar.pack(pady=20) progbar.start() root.geometry("300x150") root.title("PythonLobby.com") root.mainloop()
Output:
Explanation:
- First, we have imported all the required libraries and modules of Tkinter using import *.
- Then we have initialized root as an object for Tk() class for creating a root window.
- Here we have placed a label on our root window.
- Progress bar widget available in ttk library of Tkinter. Hence we have also imported ttk.
- Here we have used indeterminate mode for our progress bar.
- Parameters we used, orient as HORIZONTAL for the horizontal layout of our progress bar, mode as ‘determinate’ and length as 220. We can also use orient as VERTICAL for the vertical layout of our progress bar.
- To start the progress bar we also need to call inbuilt function of progressbar i.e. progbar_obj.start(). To stop progress bar we can use progbar_obj.stop().
Example 3: Example of a progress bar with a scale property in Tkinter.
from tkinter import * from tkinter import ttk root = Tk() label = Label(root, text ='Progress Bar', font = "50") label.pack(pady=5) #Progress Bar progbar = ttk.Progressbar(root, orient=HORIZONTAL, length=220, mode="determinate", max=50.0) progbar.pack(pady=20) progbar.start() progbar.stop() #Scale value = DoubleVar() progbar.config(variable=value) scale = ttk.Scale(root, orient=HORIZONTAL, length=220, var=value, from_ = 0.0, to= 50.0) scale.pack() root.geometry("300x150") root.title("PythonLobby.com") root.mainloop()
Output:
Explanation:
- First, we have imported all the required libraries and modules of Tkinter using import *.
- Then we have initialized root as an object for Tk() class for creating a root window.
- Here we have placed a label on our root window.
- Progress bar widget available in ttk library of Tkinter. Hence we have also imported ttk.
- Here we have used indeterminate mode for our progress bar.
- Parameters we used, orient as HORIZONTAL for the horizontal layout of our progress bar, mode as ‘determinate’ and length as 220. We can also use orient as VERTICAL for the vertical layout of our progress bar.
- To start the progress bar we also need to call inbuilt function of progressbar i.e. progbar_obj.start(). To stop progress bar we can use progbar_obj.stop().
- We created extra scale widget available in ttk library and configured it with the progress bar.
- Below is the code for configuration of scale with progress bar. Same code is integrated with the above example as well.
value = DoubleVar() progbar.config(variable=value) scale = ttk.Scale(root, orient=HORIZONTAL, length=220, var=value, from_ = 0.0, to= 50.0) scale.pack()