Definition of Tkinter Slider
Tkinter is a module which is used to create the GUI in python we have many different modules available in python by this is the most frequent and easy to use. A slider is available inside this Tkinter module by the use of it we can set a value. We can implement the slider vertically or horizontally depends on requirements. It will provide us one indicator which can be moved to select one value.
In python Tkinter, we can use the scale method to create a slider in python. By the use of it we can create it either vertically or horizontally. We will see its syntax for better understating how to use it. See below;
Scale(root, bg, fg, bd, command, orient, from_, to, ..)
As you can see here it takes a lot of variables as parameters. By the use of it, we can assign the orientation of the slide bar, color, starting and ending value for the slider, and so on.
We can also see one practice syntax example to understand it better see below;
Scale( parent, variable = v1, from_ = 10, to = 20, orient = VERTICLE)
This syntax gives an idea of how to use this in our program.
How does Slider Work in Tkinter?
As now we know that the slider is used to set a value. A slider can be of two type horizontal or vertical. Slider in Tkinter can be implemented by the use of scale method, in this, we assign a specific value range by using from to parameters that are available. Now we will discuss the type of slider which can be implemented see below;
- Horizontal: If we create a horizontal slider then the value can be selected by moving the indicator from left to right or vice-versa like normal horizontal looks like.
- Vertical: If we create a vertical slider then the value can be selected by moving the indicator from top to down or vice-versa like normal Vertical looks like.
In the scale method what it does internally is it will create a graphical object for us and this graphical object is nothing but an indicator or a knob we can say. By the use of this object we can move the indicator left, right, top, bottom depends upon the orientation of the slider. We can choose the orientation while calling this scaling method. This method also takes the parent object as Tkinter which is responsible to create a window for us for GUI. Now we see one practice example to use this slider in our GUI application which will be helpful for beginners also to understand how to proceed with its use. See below;
from Tkinter import * parent = Tk() sli = Scale(parent, from_=10, to=20, orient=HORIZONTAL) sli.pack() mainloop()
In the above example, we are creating one slider here. In order to use this, we need to import the Tkinter module into our program because the scale is an object available inside this module only. To import this we are using the import statement here as fromTkinter import *. After this, we are creating the Tkinter object to assign its method and object to create the GUI. Immediately after this we are calling the Scale method here and passing all the necessary parameters here which will be required to create a graphical object for us. Also, we are required to pass the parent Tk object inside the method. At the end we are calling the mainloop method to initialize the window, before this we are also calling the pack method which will be specified a position got the graphical object on the screen.
This Scala takes a lot of parameters while calling it. This includes all the necessary parameters which are required to create a slider on GUI. We can assign even color to our slider by proving value to its one of the parameters in the constructor. We will discuss all the parameters in details see below with its syntax;
Scale(parent, bg, fg, bd, command, orient, from_, to, ..)
- Orient: This parameter is used to assign the orientation for the slider it can be either of the two horizontal or vertical.
- root: It is nothing but the object of Tkinter which will act as a parent.
- label: This parameter is used to assign a label for this widget.
- state: This parameter is responsible to make our slider unresponsive or responsive.
- from: This parameter decides the starting value for the slider.
- to: This parameter decides the ending value for the slider.
- bg: By the use of this parameter we can assign background color for our widget.
- bd: By the use of this parameter we can create a border.
- fg: By the use of this parameter we can assign foreground color for our widget.
- sliderlength: This parameter decides the length of our slider.
- tickinterval: By the use of this parameter we can assign the interval value for our slider.
Scala method provides us two different methods to access the value of the slider.
- set(value): This method is used to set a value for the slider. If you want to give any default value for your slider then we can use this method.
- get: As the name suggests this method is used to get the value from the slider. If we have assigned any value to the slider then somehow we need it for future operation. So by the use of this method, we can assess the value.
Examples of Tkinter Slider
Following are the examples are given below:
In this example, we are creating a horizontal slider with red color using the scale method in Tkinter.
from tkinter import * defshowval(): print (sli1.get()) parent = Tk() parent.geometry("500x500") sli1 = Scale(parent, from_=10, to=100, tickinterval=10, orient=HORIZONTAL, bg = "RED") sli1.place(relx=0.15,rely=0.06,relwidth=10) sli1.pack() Button(parent, text='click me !!', command=showval).pack() mainloop()
In this example, we are creating a vertical slider with red color using the scale method in Tkinter.
from tkinter import * defshowval(): print (sli1.get()) parent = Tk() parent.geometry("500x500") sli1 = Scale(parent, from_=10, to=100, tickinterval=10, orient=VERTICAL, bg = "RED") sli1.place(relx=0.15,rely=0.06,relwidth=10) sli1.pack() Button(parent, text='click me !!', command=showval).pack() mainloop()
Using this scale method we can implement a slider in python. Tkinter module provides us many widgets which can be placed in the window for the user to perform different operations. A slider can be horizontal or vertical with interval can be possible in value, also the color can also be allowed.
This is a guide to Tkinter Slider. Here we also discuss the definition and How does Slider Work in Tkinter? along with different examples and its code implementation. You may also have a look at the following articles to learn more –