Python tkinter Progressbar

ttk Progressbar
We can display status of any process by using Progressbar which is part of ttk module. ( What is ttk ? )

Tkinter Progressbar to show status with options and methods to manage and display values

Code for a basic Progressbar is here.
import tkinter  as tk 
from tkinter import *
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("400x200")  

prg1 = ttk.Progressbar(my_w,orient = HORIZONTAL,
        value=70,length = 300, mode = 'determinate')
prg1.place(relx=.1,rely=.2)    
my_w.mainloop()  # Keep the window open

Progressbar Options

OptionsDetails
orientOrientation of the Progress bar. Values are Horizontal or Vertical
length Length of the Progress bar, (width if horizontal, height if vertical)
mode Values can be determinate OR indeterminate Check the examples
maximum Default value is 100, specifies the maximum value.
valueThe current value of the Progress bar. Can be set or read.
variableVariable can be linked to value of Progress Bar. Realtime changes ( in values ) can be captured.
Options : value, mode ,orient ,variable etc..

Progressbar Methods

MethodDetails
start( interval )increment the Progress bar in every interval value (50 milliseconds default )
step( amount )Default is 1 if omitted, increments in the progress bar value by given amount
stop()Stop the autoincrement mode.

start() step() and stop() Method example

Getting values of all options of Progress bar

We will get a dictionary and by using the keys we can get the set values of the options of the Progress bar.
Here we can set the values of the options also.
my_dict=ttk.Progressbar(prg1)
print(my_dict['orient']) # print the value of orient
print(my_dict['mode']) # value of mode 

Number of tasks completed

While participating in any online survey you can see one progress bar reflecting the number of questions completed and number of questions balance.

Create one window with six pair of radio buttons where user has to select one choice for each question.

The progress bar will show the progress based on number of Choices made.
Showing progress status based on Selection of Radio buttons

Scale with Progressbar

Interlinking of Progress bar & scale
import tkinter as tk
from tkinter import *
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("400x200") 

def my_upd(value):
    prg1['value'] =my_scale1.get()

prg1 = ttk.Progressbar(my_w,orient = HORIZONTAL,
        length = 320, mode = 'determinate',maximum=100)        
prg1.grid(row=0,column=0,padx=20,pady=45)        

my_scale1 = tk.Scale(my_w, from_=0, to=100, orient='horizontal'
    ,command=my_upd,length=200)
my_scale1.grid(row=1,column=0) 

my_w.mainloop()
Tkinter interlinked Progressbar and Scale to reflect scale movement as status of progressbar

Changing colour of the progress bar

We can integrate ttk.style properties to change the colour of the progress bar. We can set a default colour by adding style option.

Let us make the default colour of the progress bar as Yellow.
s = ttk.Style()
s.theme_use('alt')
s.configure("yellow.Horizontal.TProgressbar", background='yellow')

prg1=ttk.Progressbar(my_w,length=320,mode='determinate',maximum=100,value=75,
		style='yellow.Horizontal.TProgressbar')
We can integrate Button click event to update the colour of the progress bar. Here we are using config() to update the style property of the Progress bar.
Updating colour of Progress bar

Tkinter Progressbar background colour changing using button click and ttk style configuration
import tkinter as tk
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("400x300") 

s = ttk.Style()
s.theme_use('alt')
s.configure("red.Horizontal.TProgressbar", background='red')
s.configure("yellow.Horizontal.TProgressbar", background='yellow')
s.configure("green.Horizontal.TProgressbar", background='green')

l1=tk.Label(my_w,text='Changing style of Progress bar',font=16,bg='yellow')
l1.grid(row=0,column=0,columnspan=3,pady=15)

prg1=ttk.Progressbar(my_w,length=320,mode='determinate',maximum=100,value=75,
style='yellow.Horizontal.TProgressbar')
prg1.grid(row=1,column=0,columnspan=3,padx=20,pady=45) 

def my_upd(value): # update the progress bar using scale input
    prg1['value'] =my_scale1.get() 

b1=tk.Button(my_w,text='Red',bg='red',font=20,
	command=lambda:prg1.config(style='red.Horizontal.TProgressbar'))
b1.grid(row=2,column=0)
b2=tk.Button(my_w,text='Yellow',bg='yellow',font=20,
	command=lambda:prg1.config(style='yellow.Horizontal.TProgressbar'))
b2.grid(row=2,column=1)
b3=tk.Button(my_w,text='Green',bg='lightgreen',font=20,
	command=lambda:prg1.config(style='green.Horizontal.TProgressbar'))
b3.grid(row=2,column=2)

my_scale1 = tk.Scale(my_w, from_=0, to=100, orient='horizontal'
    ,command=my_upd,length=200)
my_scale1.set(75) # default value of slider kept at 75 
my_scale1.grid(row=3,column=0,columnspan=3) 
my_w.mainloop()
ProgressBar showing number of chars entered in a Text widget
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer