How to create a download progress bar in Tkinter?

TkinterPythonServer Side ProgrammingProgramming

Let’s suppose that we are creating an application which interacts with sources and files such as downloading the files, tracking the file. In order to make a progressbar for such an application, we will use the tkinter.ttk package that includes the Progressbar module.

Initially, we will instantiate an object of Progressbar which has orientation of Horizontal. Then, we will define a function to increase the value of the progressbar and keep updating it.

Example

In the following example, we have created a download progress bar by updating its value.

#Import the required libraries
from tkinter import *
from tkinter.ttk import *
import time

#Create an instance of tkinter frame
win= Tk()

#Set the geometry of frame
win.geometry("620x400")

#Define a function
def start():
   task=10
   x=0
   while(x<task):
      time.sleep(1)
      bar['value']+=10
      x+=1
      win.update_idletasks()

bar= Progressbar(win, orient=HORIZONTAL, length=300)
bar.pack(pady=20)

#Create a button
Button(win, text="Download", command=start).pack(pady=20)

win.mainloop()

Output

Running the code will display a download bar and once we click on the “Download” Button, it will automatically get completed.

raja
Published on 26-Mar-2021 10:59:38
Advertisements