# Learning Python- Intermediate course: Day 29, Sliders in Tkinter

Originally published on the dev.to platform here

# Today let us learn all about the slider widget in Tkinter


The slider widget is a widget that helps us choose values from a given range in a very interactive and graphical way.

# Making a slider widget.

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()

image
image

image
image

# Setting default value

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()

image
image

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.

image
image

# Orientation of the slider.

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()

image
image

# Getting the value from the slider.

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()

image
image

image
image

# Setting the interval length

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

image
image

While setting the value to 1 will show the entire range

image
image

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.

image
image

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()