Python Tkinter Spinbox Widget example

Published December 16, 2021

The Tkinter Spinbox enables users to choose a value from a predefined range of options. Typically, these values represent a range of integers. The spinbox widget displays the current value as well as the pair arrowheads in a segment.

When you press the up arrow, the spinbox transfers the presently selected value to the next lower value in the sequence; if the current value reaches the lowest value, it may be set as the minimum value. Furthermore, when you press the downward-pointing key, the spinbox transfers the current value to the next lower value, and if it reaches the lowest value, it may be set to be the minimum value.

Let's have a look at how to create a spinbox using Python Tkinter.

 

Syntax

To build the Tkinter spinbox, use the following syntax:

Spinbox = w (root, option1, option2, option3….)

Where root represents the parent window and option represents different attributes you can use to create the widget.

 

Options for Tkinter's Spinbox

  • State- represents the states of the widget

  • Textvariable-  this option  is used to control how the  widget behaves

  • To- specifies  the maximum  value limit 

  • Validate- specifies how the  widget will be validated

  • Validatecommand- determines the  function callback  is called  to validate  the widget  content

  • Values- specifies  the tuple  containing  the  widget  values 

  • Vcmd- this option is used as the validation command

  • Width- sets the width of the  widget

  • Wrap- this option  wraps the up and down  button  of the widget

  • Xscrollcommand- makes the widget horizontally scrollable.

  • Font- represents  the font of the text used

  • Fg-  specifies the  font color of the text

  • Format-  formats  the string texts

  • From- sets the minimum value

  • Justify- specifies how the text is justified. Text can be justified  CENTER, RIGHT, or LEFT.

  • Relief- represents the border type. However, the default value is SUNKEN. 

  • Activebackground- specifies the widget's background color when it is under the cursor.

  • Bg-  specifies the normal  background color of the widget

  • Bd- defines the border size around the indicator.  The  default  value is 2px

  • Cursor- specifies the behavior of the cursor when it hovers over the widget. 

 

Tkinter SpinBox Example

This code allows you to  select  a  particular number between  20 and 30

 

from tkinter import *

root = Tk()

root.geometry("400x300")

w = Label(root, text ='Choose a number between 20 and 30', font = "50", fg="green")

w.pack()

sp = Spinbox(root, from_= 20, to = 30)

sp.pack()

root.mainloop()

 

 

Python Spinbox how to set value

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

258 Views