20
Learning Python- Intermediate course: Day 29, Sliders in Tkinter
The slider widget is a widget that helps us choose values from a given range in a very interactive and graphical way.
We can make the slider widget using the following syntax
slider=Scale(master, from_=0, to=10)
Parameters
-
master
The main Tk() window. -
from_
The starting value of the slider -
to
the ending value of the slider.
from tkinter import *
frame=Tk()
frame.geometry("200x200")
slider=Scale(frame, from_=0, to=10)
slider.pack()
mainloop()
The slider.set()
is a method which sets the value of a slider. We can use this to initialize the default value of the slider.
from tkinter import *
frame=Tk()
frame.geometry("200x200")
slider=Scale(frame, from_=0, to=10)
slider.set(2)
slider.pack()
mainloop()
The set()
method can also be used to dynamically set the value of the slider variable.
The program below is an example of the set() method. There is a button which runs a function which resets the value of the slider back to the default values when pressed.
from tkinter import *
frame=Tk()
frame.geometry("200x200")
slider=Scale(frame, from_=0, to=10)
slider.set(2)
slider.pack()
def reset():
slider.set(2)
resetbutton=Button(frame, text="reset",command=reset)
resetbutton.pack()
mainloop()
On pressing the reset button, the value returns back to the default value.
By default, the slider is vertical. But we can use the orient
property to set the value of orientation.
from tkinter import *
frame=Tk()
frame.geometry("200x200")
slider=Scale(frame,from_=0, to=10, orient="horizontal")
slider.set(2)
slider.pack()
def reset():
slider.set(2)
resetbutton=Button(frame,text="reset",command=reset)
resetbutton.pack()
mainloop()
We can get the value of the slider using the slider.get()
method. The program below shows how to get the values of the slider using buttons.
from tkinter import *
frame=Tk()
frame.geometry("200x200")
slider=Scale(frame,from_=0, to=10, orient="horizontal")
slider.set(2)
slider.pack()
def reset():
resetbutton.config(text=slider.get())
resetbutton=Button(frame,text="show",command=reset)
resetbutton.pack()
mainloop()
We can display the values of the sliders using the tickinterval
property. We can adjust the interval values of the slider using the tickinterval
attribute. For example, setting the value to 10 equal will show the result as
While setting the value to 1 will show the entire range
from tkinter import *
frame=Tk()
frame.geometry("200x200")
slider=Scale(frame,from_=0, to=10, tickinterval=1, orient="horizontal")
slider.set(2)
slider.pack()
def reset():
resetbutton.config(text=slider.get())
resetbutton=Button(frame,text="show",command=reset)
resetbutton.pack()
mainloop()
That looked a bit crowded. Didn't it? We can adjust it using the length property of the slider.
Here is the code-
from tkinter import *
frame=Tk()
frame.geometry("200x200")
slider=Scale(frame,from_=0, to=10, tickinterval=1, length= 500, orient="horizontal")
slider.set(2)
slider.pack()
def reset():
resetbutton.config(text=slider.get())
resetbutton=Button(frame,text="show",command=reset)
resetbutton.pack()
mainloop()
So friends that was all for today. Tomorrow we will learn about the spinbox widget, and the day after that we will implement a program using the both widgets.
20