In this Tkinter Tutorial we are going to learn about TKinter ProgressBar, tkinter 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.
This is the complete code for this lesson.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import tkinter as tk from tkinter import ttk class Window(tk.Tk): def __init__(self): super(Window, self).__init__() self.title("ProgressBar Example TKinter") self.minsize(400,350) self.wm_iconbitmap("myicon.ico") self.label_frame = ttk.LabelFrame(self, text = "ProgressBar Example") self.label_frame.grid(column = 0, row = 0) self.create_progressbar() def create_progressbar(self): self.button1 = ttk.Button(self.label_frame, text = "Start ProgressBar", command = self.start_progress) self.button1.grid(column = 0, row = 0) self.button2 = ttk.Button(self.label_frame, text="Stop ProgressBar", command = self.stop_progress) self.button2.grid(column=0, row=2) self.progress_bar = ttk.Progressbar(self, orient = 'horizontal', length = 280, mode = 'determinate') self.progress_bar.grid(column = 0, row = 3, pady = 10) def start_progress(self): self.progress_bar.start() def stop_progress(self): self.progress_bar.stop() window = Window() window.mainloop() |
In here we have created two buttons, one for starting of the progressbar and the second for stopping the progressbar, we have connected buttons command with the specific methods of start_progress() and stop_progress(), these are the method for starting and stopping of the ProgressBar. also add these buttons in a grid layout.
1 2 3 4 5 6 7 |
self.button1 = ttk.Button(self.label_frame, text = "Start ProgressBar", command = self.start_progress) self.button1.grid(column = 0, row = 0) self.button2 = ttk.Button(self.label_frame, text="Stop ProgressBar", command = self.stop_progress) self.button2.grid(column=0, row=2) |
This is our ProgressBar in TKinter, you can use ProgressBar from ttk, even tough we have the progressbar in tkinter but it is old style, you need to add the orientation for the progressbar also the lenght. and at the end add the ProgressBar in the GridLayout.
1 2 3 |
self.progress_bar = ttk.Progressbar(self, orient = 'horizontal', length = 280, mode = 'determinate') self.progress_bar.grid(column = 0, row = 3, pady = 10) |
And these are the two method for starting and stopping of the Tkinter ProgressBar, we have already connected these methods with the specific button.
1 2 3 4 5 6 |
def start_progress(self): self.progress_bar.start() def stop_progress(self): self.progress_bar.stop() |
Run the complete code and this is the result.