Home » Python Tkinter Spinbox

Python Tkinter Spinbox

Oracle Java Certification
Spring Framework Basics Video Course
Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer I [1Z0-815] Practice Tests
1 Year Subscription

The Spinbox widget is used to select from a fixed number of values. It is an alternative Entry widget and provides the range of values to the user, the values are at the side of the box and there are up and down arrows where the user can navigate up and down.

Table of Contents

Syntax

w = Spinbox(top, options)

A list of possible options is given below.

Option Description
activebackground The background color of the widget when it has the focus.
bg The background color of the widget.
bd The border width of the widget.
command The associated callback with the widget which is called each time the state of the widget is called.
cursor The mouse pointer is changed to the cursor type assigned to this option.
disabledbackground The background color of the widget when it is disabled.
disabledforeground The foreground color of the widget when it is disabled.
fg The normal foreground color of the widget.
font The font type of the widget content.
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 This option used to control how the text is justified: CENTER, LEFT, or RIGHT. The default is LEFT.
relief It is used to specify the type of the border. The default is SUNKEN.
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”.
textvariable It is like a control variable which is used to control the behaviour of the widget text.
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 This option is associated to the function callback which is used for the validation of the widget content.
values It represents the tuple containing the values for this widget.
vcmd It is same as validatecommand.
width It represents the width of the widget.
wrap This option wraps up the up and down button the Spinbox.
xscrollcommand This 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.

Option Description
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.

 

Examples

Here is an example

from tkinter import *

master = Tk()

w = Spinbox(master, from_=0, to=10)
w.pack()

mainloop()

here is another basic example

from tkinter import *
  
root = Tk()
root.geometry("300x200")
  
w = Label(root, text ='maxpython', font = "50") 
w.pack()
  
sp = Spinbox(root, from_= 0, to = 20)
sp.pack()
  
root.mainloop() 

This will display the following

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More