Python Tkinter Spinbox

Created with Sketch.

Python Tkinter Spinbox

The Spinbox widget is an alternative to the Entry widget. It provides the range of values to the user, out of which, the user can select the one.

It is used in the case where a user is given some fixed number of values to choose from.

We can use various options with the Spinbox to decorate the widget. The syntax to use the Spinbox is given below.

Syntax

  1. w = Spinbox(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 width of the widget.
4commandThe associated callback with the widget which is called each time the state of the widget is called.
5cursorThe mouse pointer is changed to the cursor type assigned to this option.
6disabledbackgroundThe background color of the widget when it is disabled.
7disabledforegroundThe foreground color of the widget when it is disabled.
8fgThe normal foreground color of the widget.
9fontThe font type of the widget content.
10formatThis option is used for the format string. It has no default value.
11from_It is used to show the starting range of the widget.
12justifyIt is used to specify the justification of the multi-line widget content. The default is LEFT.
13reliefIt is used to specify the type of the border. The default is SUNKEN.
14repeatdelayThis option is used to control the button auto repeat. The value is given in milliseconds.
15repeatintervalIt is similar to repeatdelay. The value is given in milliseconds.
16stateIt represents the state of the widget. The default is NORMAL. The possible values are NORMAL, DISABLED, or “readonly”.
17textvariableIt is like a control variable which is used to control the behaviour of the widget text.
18toIt specify the maximum limit of the widget value. The other is specified by the from_ option.
19validateThis option controls how the widget value is validated.
20validatecommandIt is associated to the function callback which is used for the validation of the widget content.
21valuesIt represents the tuple containing the values for this widget.
22vcmdIt is same as validation command.
23widthIt represents the width of the widget.
24wrapThis option wraps up the up and down button the Spinbox.
25xscrollcommandThis options is set to the set() method of scrollbar to make this widget horizontally scrollable.

Methods

There are the following methods associated with the widget.

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

Example

  1. from tkinter import *
  2. top = Tk()
  3. top.geometry(“200×200”)
  4. spin = Spinbox(top, from_= 0, to = 25)
  5. spin.pack()
  6. top.mainloop()

Output:

Python Tkinter Spinbox

 

Leave a Reply

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