Python Tkinter Scale

python tutorials and learn python

Created with Sketch.

Python Tkinter Scale

The Scale widget is used to implement the graphical slider to the python application so that the user can slide through the range of values shown on the slider and select the one among them.

We can control the minimum and maximum values along with the resolution of the scale. It provides an alternative to the Entry widget when the user is forced to select only one value from the given range of values.

The syntax to use the Scale widget is given below.

Syntax

  1. w = Scale(top, options)

A list of possible options is given below.

SNOptionDescription
1activebackgroundThe background color of the widget when it has the focus.
2bgThe background color of the widget.
3bdThe border size of the widget. The default is 2 pixel.
4commandIt is set to the procedure which is called each time when we move the slider. If the slider is moved rapidly, the callback is done when it settles.
5cursorThe mouse pointer is changed to the cursor type assigned to this option. It can be an arrow, dot, etc.
6digitsIf the control variable used to control the scale data is of string type, this option is used to specify the number of digits when the numeric scale is converted to a string.
7fontThe font type of the widget text.
8fgThe foreground color of the text.
9from_It is used to represent one end of the widget range.
10highlightbackgroundThe highlight color when the widget doesn’t have the focus.
11highlighcolorThe highlight color when the widget has the focus.
12labelThis can be set to some text which can be shown as a label with the scale. It is shown in the top left corner if the scale is horizontal or the top right corner if the scale is vertical.
13lengthIt represents the length of the widget. It represents the X dimension if the scale is horizontal or y dimension if the scale is vertical.
14orientIt can be set to horizontal or vertical depending upon the type of the scale.
15reliefIt represents the type of the border. The default is FLAT.
16repeatdelayThis option tells the duration up to which the button is to be pressed before the slider starts moving in that direction repeatedly. The default is 300 ms.
17resolutionIt is set to the smallest change which is to be made to the scale value.
18showvalueThe value of the scale is shown in the text form by default. We can set this option to 0 to suppress the label.
19sliderlengthIt represents the length of the slider window along the length of the scale. The default is 30 pixels. However, we can change it to the appropriate value.
20stateThe scale widget is active by default. We can set this to DISABLED to make it unresponsive.
21takefocusThe focus cycles through the scale widgets by default. We can set this option to 0 if we don’t want this to happen.
22tickintervalThe scale values are displayed on the multiple of the specified tick interval. The default value of the tickinterval is 0.
23toIt represents a float or integer value that specifies the other end of the range represented by the scale.
24troughcolorIt represents the color of the through.
25variableIt represents the control variable for the scale.
26widthIt represents the width of the through part of the widget.

Methods

SNMethodDescription
1get()It is used to get the current value of the scale.
2set(value)It is used to set the value of the scale.

Example

  1. from tkinter import *
  2. def select():
  3.    sel = “Value = “ + str(v.get())
  4.    label.config(text = sel)
  5. top = Tk()
  6. top.geometry(“200×100”)
  7. v = DoubleVar()
  8. scale = Scale( top, variable = v, from_ = 1, to = 50, orient = HORIZONTAL)
  9. scale.pack(anchor=CENTER)
  10. btn = Button(top, text=“Value”, command=select)
  11. btn.pack(anchor=CENTER)
  12. label = Label(top)
  13. label.pack()
  14. top.mainloop()

Output:

Python Tkinter Scale

 

Leave a Reply

Your email address will not be published. Required fields are marked *