Scale widget creates a graphical object, which allows the user to select a numerical value by moving a slider knob along a scale of a range of values. Thus using this Tkinter object a user can set a value by moving an indicator. The minimum and maximum values can be set as parameters, as well as the resolution. We can also determine if we want the slider vertically or horizontally positioned. A Scale widget is a good alternative to an Entry widget, if the user is supposed to put in a number from a finite range, i.e. a bounded numerical value.
To create a scale with a specified range, use the from and to options. Note that to pass the from option as a keyword argument, you need to add a trailing underscore (from is a reserved keyword in Python). The following code creates a slider scale widget:
import tkinter as tk
from tkinter import *
root = Tk()
scale = Scale(from_=1, to_=100, length=300, orient='horizontal')
scale.pack()
root.mainloop()
To create a scale with a specified range, use the from and to options. Note that to pass the from option as a keyword argument, you need to add a trailing underscore (from is a reserved keyword in Python). The following code creates a slider scale widget:
import tkinter as tk
from tkinter import *
root = Tk()
scale = Scale(from_=1, to_=100, length=300, orient='horizontal')
scale.pack()
root.mainloop()
The output of the program is shown below:
If we don't specify the value for orient then the default orientation is vertical. The following program shows a scale widget with two sliders:
import tkinter as tk
from tkinter import *
root = Tk()
scale = Scale(from_=1, to_=100, length=300)
scale1 = Scale(from_=1, to_=100, length=300, orient='horizontal')
scale.pack()
scale1.pack()
root.mainloop()
The output of the program is shown below:
Some of the useful options of the Scale widget are:
from_ - minimum value possible by dragging the scale
to_ - maximum value possible by dragging the scale
length - how many pixels long the scale is
label - specify a label for the scale
showvalue='NO' - gets rid of the number that displays above the scale
tickinterval=1 - displays tickmarks at every unit (1 can be changed)
There are several ways for your program to interact with the scale. One way is to link it with an IntVar just like with check buttons and radio buttons, using the variable option. Another option is to use the scale’s get and set methods. A third way is to use the command option, which works just like with buttons.
Accessing Slider Values
We can access slider values with the get method. We modify the previous example with a Button to view the values. If this button is pushed, the values of both sliders is printed into the terminal from which we have started the script. See the code below:
import tkinter as tk
from tkinter import *
def show_values():
print (scale.get(), scale1.get())
root = Tk()
scale = Scale(from_=1, to_=100, length=300)
scale1 = Scale(from_=1, to_=100, length=300, orient='horizontal')
scale.pack()
scale1.pack()
Button(root, text='Show', command=show_values).pack()
root.mainloop()
import tkinter as tk
from tkinter import *
root = Tk()
scale = Scale(from_=1, to_=100, length=300)
scale1 = Scale(from_=1, to_=100, length=300, orient='horizontal')
scale.pack()
scale1.pack()
root.mainloop()
The output of the program is shown below:
Some of the useful options of the Scale widget are:
from_ - minimum value possible by dragging the scale
to_ - maximum value possible by dragging the scale
length - how many pixels long the scale is
label - specify a label for the scale
showvalue='NO' - gets rid of the number that displays above the scale
tickinterval=1 - displays tickmarks at every unit (1 can be changed)
There are several ways for your program to interact with the scale. One way is to link it with an IntVar just like with check buttons and radio buttons, using the variable option. Another option is to use the scale’s get and set methods. A third way is to use the command option, which works just like with buttons.
Accessing Slider Values
We can access slider values with the get method. We modify the previous example with a Button to view the values. If this button is pushed, the values of both sliders is printed into the terminal from which we have started the script. See the code below:
import tkinter as tk
from tkinter import *
def show_values():
print (scale.get(), scale1.get())
root = Tk()
scale = Scale(from_=1, to_=100, length=300)
scale1 = Scale(from_=1, to_=100, length=300, orient='horizontal')
scale.pack()
scale1.pack()
Button(root, text='Show', command=show_values).pack()
root.mainloop()
The output of the program is shown below:
You may verify the values in the command window and the position of the slider in the widget.
Initializing Sliders
A slider starts with the minimum value, which is 1 in our examples. The following program initialize Sliders using the set(value) method:
import tkinter as tk
from tkinter import *
root = Tk()
scale = Scale(from_=1, to_=100, length=300)
scale1 = Scale(from_=1, to_=100, length=300, orient='horizontal')
scale.set(20)
scale.pack()
scale1.set(25)
scale1.pack()
root.mainloop()
The output of the program is shown below:
As we can notice, the sliders now start at the specified positions in the program.
tickinterval
If the option tickinterval is set to a number, the ticks of the scale will be displayed as multiples of that value. The following program uses the tickinterval option:
import tkinter as tk
from tkinter import *
root = Tk()
scale = Scale(from_=0, to_=100, length=300,tickinterval=5)
scale.pack()
root.mainloop()
The output of the program is shown below:
Here I am ending today's post. See you again with a new topic, till then keep practicing and learning Python as Python is easy to learn!
You may verify the values in the command window and the position of the slider in the widget.
Initializing Sliders
A slider starts with the minimum value, which is 1 in our examples. The following program initialize Sliders using the set(value) method:
import tkinter as tk
from tkinter import *
root = Tk()
scale = Scale(from_=1, to_=100, length=300)
scale1 = Scale(from_=1, to_=100, length=300, orient='horizontal')
scale.set(20)
scale.pack()
scale1.set(25)
scale1.pack()
root.mainloop()
The output of the program is shown below:
As we can notice, the sliders now start at the specified positions in the program.
tickinterval
If the option tickinterval is set to a number, the ticks of the scale will be displayed as multiples of that value. The following program uses the tickinterval option:
import tkinter as tk
from tkinter import *
root = Tk()
scale = Scale(from_=0, to_=100, length=300,tickinterval=5)
scale.pack()
root.mainloop()
The output of the program is shown below:
Here I am ending today's post. See you again with a new topic, till then keep practicing and learning Python as Python is easy to learn!
0 comments:
Post a Comment