Tkinter

Spinbox Tkinter | Python 3

In this tutorial, we are going to see how to use Spinbox in Tkinter. The Spinbox widget can be used in place of Entry, in cases where the user has only a limited number of values to choose from.

Note that the Spinbox widget is only available in Python 2.3. Also, note that several Tk spinbox methods appear to be missing in Python 2.3.
 


 
 

Syntax:

Here is the syntax to create this widget:

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

 

Parameters:
  • master : This represents the parent window.
  • options : Options can be used as comma-separated key-value. See the Spinbox widget options below:

 

Example:
from tkinter import *

gui = Tk()

sp = Spinbox(gui, from_=0, to=15)
sp.pack()

gui.mainloop()

Output:


 
 

Spinbox widget options
Option
Description
bd Border width in pixels. The default value is 2.
bg Background color.
command Function or method to call each time the user changes the state of the Spinbox widget.
fg Foreground color (text).
font Text font to use for the Spinbox widget.
justify
  • LEFT to justify the text on the left of each line;
  • CENTER to center the text;
  • RIGHT to justify the text on the right.
from_ The minimum value. Used with ‘to’ to limit the spinbox range.
to The maximum value. Used with ‘from_’ to limit the spinbox range.
values A tuple containing valid values for this widget. Replace from/to/increment.
textvariable In order to get the current value of your spinbox widget, you need to set this option on an instance of the StringVar class.
relief Relief indicates the type of border. Some of the values are SUNKEN, RAISED, GROOVE and RIDGE.
validate Validation mode. The default value is NONE.
width The default width of the Spinbox widget is determined by the size of the character displayed. You can set this option to a number of characters.
xscrollcommand Used to connect a Spinbox field to a horizontal scrollbar. This option must be set to set() method of the corresponding scrollbar.

 

 

Methods:

Here are the commonly used methods for this widget:

Method
Description
get(startindex [,endindex]) This method returns a specific character or a range of text.
delete(startindex [,endindex]) This method deletes a specific character or a range of text.
identify(x, y) Identifies the element at the given location.
index(index) Returns the absolute value of an index based on the given index.
insert(index [,string]…) This method inserts strings at the specified index location.
invoke(element) Invokes the Spinbox button.

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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