Python tkinter scale

Displaying a scale .
Basics of Python Tkinter

Tkinter Scale to set and get value by moving slider with orient & other options and methods
import tkinter as tk 
my_w = tk.Tk()
my_w.geometry("200x200") 
my_w.title("Scale")
l1=tk.Label(my_w,text="Scale")
l1.grid(row=1,column=1)

my_scale = tk.Scale(my_w, from_=0, to=100, orient='horizontal')
my_scale.grid(row=2,column=1) 

my_w.mainloop())
Scale Tkinter
Reading value of a scale using get()
my_scale.get()
Setting value of a scale using set()
my_scale.set()
We can assign the default value to our scale by using set().
my_scale.set(75)

Copy value from one scale to other by button click

By using a button click we will assign the value of 2nd scale by reading value of 1st.
Read and set value of Scale
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") 

def my_upd():
    my_scale2.set(my_scale1.get()) # read and set value 
    
my_scale1 = tk.Scale(my_w, from_=0, to=100, orient='horizontal')
my_scale1.grid(row=1,column=1) 

my_scale2 = tk.Scale(my_w, from_=0, to=100, orient='horizontal')
my_scale2.grid(row=3,column=1) 

b1 = tk.Button(my_w, text='Copy', width=10,command=lambda: my_upd())
b1.grid(row=2,column=1) 

my_w.mainloop()

Direct Copy from one Scale to other without any button click

On move copy data from Scale
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("300x200") 

def my_upd(value):
    my_scale2.set(my_scale1.get())
        
my_scale1 = tk.Scale(my_w, from_=0, to=100, orient='horizontal',command=my_upd)
my_scale1.grid(row=1,column=1) 

my_scale2 = tk.Scale(my_w, from_=0, to=100, orient='horizontal')
my_scale2.grid(row=3,column=1) 

my_w.mainloop()

Inter connecting Spinbox and Scale widgets

We can add one Spinbox widget and link it to a Scale. Here both are connected so changes in any one will reflect in both.
We used one common StringVar sv here.
Connected Scale and Spinbox
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("300x150")  # Size of the window 
my_w.title("www.plus2net.com")  # Adding a title

sv = StringVar() #string variable 
sb = Spinbox(my_w,textvariable=sv, width=15,from_=0,to=100)
sb.grid(row=1,column=1,padx=50,pady=10)

sc = Scale(my_w, from_=0, to=100,orient=HORIZONTAL,variable=sv)
sc.grid(row=2,column=1,padx=50)

my_w.mainloop()  # Keep the window open

Enable disable scale

We can set the option state to active, disabled & normal
sv = StringVar() #string variable 
sc = Scale(my_w, from_=0, to=100,orient=HORIZONTAL,
		state='disabled',variable=sv)
sc.grid(row=2,column=1,padx=50)
By using config() we can change the state also.
sc.config(state='normal')

Options

We can use these options for configuration of Scale.
activebackgroundColour of the scale ( handle ) when mouse is over it.
bgBackground color of the scale area.
bdWidth of the borders
commandFunction we can use when value is changed. ( see the examples above )
cursorShape of the cursor when mouse is over it. For a list of available shapes check the end of Button tutorial
fontAssign font style , size to user entry data ( not the list ).

To change the font style of list box use this.
font1=('times',18,'bold') # set font style
sc = Scale(my_w, from_=0, to=100,
variable=sv,orient='horizontal',
activebackground='red',bg='yellow',
bd=5,fg='green',font=font1,cursor='cross')
Options of Scale widget
length Dimension of the slider, it is x value if orientation is horizontal, otherwise y value of vertical orientation.
sc = Scale(my_w, from_=1, to=200,variable=sv,
    orient='horizontal', length=180)
orientOrientation of the scale, values can be horizontal or vertical. Default is vertical. See examples above.
vertical orient option
reliefStyle of the border. Check the button section for more about relief.
It can take these values raised , sunken ,flat, ridge, solid & groove
sc = Scale(my_w, from_=1, to=200,variable=sv,
    orient='horizontal',length=180, relief='sunken')
resolutionStep value for the slider movement.
statenormal | active | disabled , options for the state. Check the example above
sliderlengthThe length of the slider
sc = Scale(my_w, from_=0, to=100,variable=sv,
	orient='horizontal',sliderlength=10)
sliderlength option of the slider
showvalueSet it to 0 if you don't want the slider value to be displayed.
takefocusSet to to 0 if you don't want it to be on focus through tab cycle along with other widgets.
widthDefault value is 15 pixel, Check the width in different orientation. ( see examples above )

Dynamic value for from_ and to

The range of value we can create by using any source ( database, excel etc ) and accordingly set the data for from_ and to.

Here we have used math floored quotient to get these values.
l1=[40,45,55,43,41,59,64,69,70] # data source 
if(min(l1)%10==0):
    my_from=min(l1)
else:
    my_from=(min(l1)//10)*10
if(max(l1)%10 == 0):
    my_to=max(l1)
else:
    my_to=((max(l1)//10)+1)*10
print(my_from,my_to)
sc2=tk.Scale(my_w,from_=my_from,to=my_to,resolution=5,
    length=150,orient='horizontal',font=font1)
Note that we have used resultion=5 so the pointer moves at a step value of 5.

Exercise

By using a pair of radio buttons, change the state of a scale.

RGB colour sliders

RGB scale to get Colour code

Using colour scales to generate RGB value and update Background of a button.

Dashboard using scale

Scale value as pointer to arc in Canvas

Using Scale slider value, pointer movement over Dashboard using Canvas.

Selection of Hour Minute and Seconds using Scale

Drop down Calendar with time sliders

Set Hour Minute and Seconds in a Calendar drop down .

Creating transparent background window

Creating Transparent window with  sliders

Our scale will set the value of parent window alpha attribute within a range.
my_w.attributes('-alpha',my_scale.get())
By chaging the value of the scale from 0 to 1 with a resolution of 0.05, we can change the background transparency of the window.
Tkinter scale to manage the alpha value of the parent window attribute to make it transparent
import tkinter as tk # Python 3
my_w = tk.Tk()
my_w.geometry("415x200+410+100")  
my_img = tk.PhotoImage(file = "D:/images/top2-trans.png") 
l1=tk.Label(my_w,image=my_img)
l1.grid(row=1,column=1,padx=100,pady=10)
#my_w.overrideredirect(True)
def my_upd(value):
    my_w.attributes('-alpha',my_scale.get())

my_scale=tk.Scale(my_w,from_=0.0, to=1.0,resolution=0.05,
     command=my_upd,orient='horizontal', length=200)
my_scale.grid(row=2,column=1)

my_scale.set(1.0)
my_w.bind('<Escape>',lambda e:my_w.quit()) 
#my_w.bind('<Button-3>',lambda e:my_w.quit()) # mouse right click
my_w.mainloop()
Python Tkinter Projects Linking Progress bar & Scale
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-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer