import tkinter as tk
my_w = tk.Tk() # parent window
my_w.geometry("200x200") # width and height of the window
my_w.title("Scale") # title for the window
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,padx=10)
my_w.mainloop() # Keep the window open
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)
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()
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()
import tkinter as tk
my_w = tk.Tk() # Parent window
my_w.geometry("300x150") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
font1=['Arial',28,'normal'] # higher size font
sv = tk.StringVar() #string variable connecting both
sb = tk.Spinbox(my_w,textvariable=sv, width=5,from_=0,to=100, font=font1)
sb.grid(row=1,column=1,padx=50,pady=10)
sc = tk.Scale(my_w, from_=0, to=100,orient='horizontal',variable=sv,font=font1)
sc.grid(row=2,column=1,padx=20)
my_w.mainloop() # Keep the window open
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 a pair of Radiobuttons we can change the state of a scale. sc.config(state='normal')
import tkinter as tk
my_w = tk.Tk() # Parent window
my_w.geometry("300x150") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
font1=['Arial',22,'normal'] # higher size font
r1_v = tk.StringVar(value='normal') # variable for Radiobutton
def my_upd():
if r1_v.get()=='normal': # normal redio is clicked
sc.config(state='normal') # update Scale to normal
elif r1_v.get()=='disabled':
sc.config(state='disabled') # Disable Scale
r1 = tk.Radiobutton(my_w, text='normal', variable=r1_v,
value='normal',font=font1,command=my_upd)
r1.grid(row=1,column=1)
r2 = tk.Radiobutton(my_w, text='Disabled', variable=r1_v,
value='disabled',font=font1,command=my_upd)
r2.grid(row=1,column=2)
sc = tk.Scale(my_w, from_=0, to=100,
orient='horizontal',font=font1)
sc.grid(row=2,column=1,padx=20)
my_w.mainloop() # Keep the window open
activebackground | Colour of the scale ( handle ) when mouse is over it. |
bg | Background color of the scale area. |
bd | Width of the borders |
command | Function we can use when value is changed. ( see the examples above ) |
cursor | Shape of the cursor when mouse is over it. For a list of available shapes check the end of Button tutorial |
font | Assign font style , size to user entry data ( not the list ). To change the font style of list box use this.
|
length | Dimension of the slider, it is x value if orientation is horizontal, otherwise y value of vertical orientation.
|
orient | Orientation of the scale, values can be horizontal or vertical. Default is vertical. See examples above. ![]() |
relief | Style of the border. Check the button section for more about relief.
It can take these values raised , sunken ,flat, ridge, solid & groove
|
resolution | Step value for the slider movement. |
state | normal | active | disabled , options for the state. Check the example above |
sliderlength | The length of the slider
![]() |
showvalue | Set it to 0 if you don't want the slider value to be displayed. |
takefocus | Set to to 0 if you don't want it to be on focus through tab cycle along with other widgets. |
width | Default value is 15 pixel, Check the width in different orientation. ( see examples above ) |
import tkinter as tk
my_w = tk.Tk() # parent window
my_w.geometry("200x200") # width and height of the window
my_w.title("Scale") # title for the window
l1=tk.Label(my_w,text="Scale")
l1.grid(row=1,column=1)
font1=['Arial',22,'normal']
l1=[20,45,55,43,41,59,64,69,70] # data source , update these values
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)
sc2.grid(row=2,column=1)
my_w.mainloop() # Keep the window open
Note that we have used resolution=5 so the pointer moves at a step value of 5.
my_w.attributes('-alpha',my_scale.get())
By changing the value of the scale from 0 to 1 with a resolution of 0.05, we can change the background transparency of the window.
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()