Best post on Spinbox and Scale Widget in Python

Spinbox and Scale are widgets of tkinter in Python

Tkinter provides many widgets for GUI in Python.

Spinbox Widget

A variant of the Entry widget for selecting values from a range or an ordered set.
Spinbox widget can be used to select from a fixed number of values.
Spinbox has an area for displaying the current values, and a pair of up and down arrow to spin values.
Syntax
Here is the simple syntax to create this widget −

w = Spinbox( master, option=value, ... )

Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.

Various options of Spinbox widget:

  • activebackground: The background color of the widget when it has the focus.
  • bg : The normal background color displayed behind the spinbox and indicator.
  • bd : The size of the border around the indicator. Default is 2 pixels.
  • cursor : If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the label.
  • command : It is used to call the function associated with widget.
  • disabledbackground : The background color of the widget when it is disabled.
  • disabledforeground : The foreground color of the widget when it is disabled.
  • font : If you are displaying text in this widget(with the text or textvariable option, the font option specifies in what font that text will be displayed.
  • fg : It is used to give foreground colour.
  • format : This option is used for the format string. It has no default value.
  • from_ : It is used to show the starting range of the widget.
  • justify : It is used to specify the justification of the multi-line widget content. The default is LEFT.
  • repeatdelay : This option is used to control the button auto repeat. The value is given in milliseconds.
  • repeatinterval : It is similar to repeatdelay. The value is given in milliseconds.
  • state : It represents the state of the widget. The default is NORMAL. The possible values are NORMAL, DISABLED, or “readonly”.
  • to : It specify the maximum limit of the widget value. The other is specified by the from_ option.
  • validate: This option controls how the widget value is validated.
  • validatecommand : It is associated to the function callback which is used for the validation of the widget content.
  • text : To display one or more lines of text in a widget, set this option to a string containing the text. Internal newlines (“n”) will force a line break.
  • textvariable : To slave the text displayed in a widget to a control variable of class StringVar, set this option to that variable.
  • values : It represents the tuple containing the values for this widget.
  • width: Width of the widget in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
  • relief : Specifies the appearance of a decorative border around the widget. The default is FLAT; for other values.
  • xscrollcommand : This options is set to the set() method of scrollbar to make this widget horizontally scrollable.

Various methods of Spinbox widget:

  • delete(startindex, endindex) : This method is used to delete the characters present at the specified range.
  • get(startindex, endindex) : It is used to get the characters present in the specified range.
  • identify(x, y) : It is used to identify the widget’s element within the specified range.
  • index(index) : It is used to get the absolute value of the given index..
  • insert(index, string) : This method is used to insert the string at the specified index.
  • invoke(element) : It is used to invoke the callback associated with the widget.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :6th April 2020

Description : Program to show use of Spinbox widget

"""
from tkinter import *
root=Tk()
itv=Spinbox(root, from_=0, to=20, fg="red", bg="yellow", font="Verdana")
itv.pack() 

itv1=Spinbox(values=(1,3,5,7,9), width=35, relief=RAISED, fg="green", bg="yellow", font="Verdana")
itv1.pack()

itv2=Spinbox(values=(1,3,5,7,9), state=DISABLED, font="Verdana")
itv2.pack()

OUTPUT

Output of Spinbox Widget (itvoyagers.in)

Scale Widget

Scale widget provides an alternative to the Entry widget when the user is forced to select only one value from the given range of values.
The Scale widget is used to implement the graphical slider to the python application.
This widget helps the user to 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.
Scale can be vertically or horizontally arranged.
A scale is created with the Scale method().
Syntax
Here is the simple syntax to create this widget −

w = Scale( master, option=value, ... )

Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.

Various options of Scale widget:

  • activebackground: The background color of the widget when it has the focus.
  • bg : The normal background color displayed behind the spinbox and indicator.
  • bd : The size of the border around the indicator. Default is 2 pixels.
  • cursor : If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the label.
  • digits : If 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.
  • font : If you are displaying text in this widget(with the text or textvariable option, the font option specifies in what font that text will be displayed.
  • fg : It is used to give foreground colour.
  • from_ : It is used to show the starting range of the widget.
  • highlightbackground : The highlight color when the widget doesn’t have the focus.
  • highlighcolor : The highlight color when the widget has the focus.
  • label : This 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.
  • length : It represents the length of the widget. It represents the X dimension if the scale is horizontal or y dimension if the scale is vertical.
  • to : It specify the maximum limit of the widget value. The other is specified by the from_ option.
  • orient : It can be set to horizontal or vertical depending upon the type of the scale.
  • repeatdelay : This option tells the duration up to which the button is to be pressed before the slider starts moving in that direction repeatedly.
  • resolution : It is set to the smallest change which is to be made to the scale value.
  • textvariable : To slave the text displayed in a widget to a control variable of class StringVar, set this option to that variable.
  • showvalue : The value of the scale is shown in the text form by default. We can set this option to 0 to suppress the label.
  • width: Width of the widget in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
  • relief : Specifies the appearance of a decorative border around the widget. The default is FLAT; for other values.
  • sliderlength : It 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.
  • state : The scale widget is active by default. We can set this to DISABLED to make it unresponsive.
  • takefocus : The focus cycles through the scale widgets by default. We can set this option to 0 if we don’t want this to happen.
  • tickinterval : The scale values are displayed on the multiple of the specified tick interval. The default value of the tickinterval is 0.
  • troughcolor : It represents the color of the through.
  • variable : It represents the control variable for the scale.

Various methods of Scale widget:

  • get() : It is used to get the current value of the scale.
  • set(value) : It is used to set the value of the scale.
  • identify(x, y) : It is used to identify the widget’s element within the specified range.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :6th April 2020

Description : Program to show use of Scale widget

"""
from tkinter import *

def display():
    print (itv1.get(), itv2.get())

master = Tk()
itv1 = Scale(master, from_=0, to=200,length=800,tickinterval=10, orient=HORIZONTAL,fg="orange",bg="black",font="verdana")
itv1.set(23)
itv1.pack(anchor=CENTER)

itv2 = Scale(master, from_=0, to=41, tickinterval=8,fg="cyan",bg="black",font="verdana")
itv2.set(19)
itv2.pack(anchor=CENTER)

Button(master, text='Selected Values', command=display).pack()


OUTPUT

Output of Scale widget (itvoyagers.in)

You can also check following posts to know more about tkinter and GUI

Leave a Comment