Tkinter Spinbox

Tkinter Spinbox Widget

The Tkinter Spinbox widget is the advanced version of the Entry widget. In the Spinbox widget, a user can choose a value from a list of fixed values by clicking the “up arrow” or “down arrow” button and the user can also type inside the Tkinter Spinbox widget. And you can also give a range of numbers to the Tkinter Spinbox widget so users can choose a number by clicking the arrow buttons.

Below are some specific and mostly used configuration options of the Tkinter Spinbox widget.
from_This option can help you to specify the lowest value or starting value of the Spinbox widget. But if you are using the “values” option too, this option will have no effect.
toWith the help of this option you specify the end value or highest value of the Spinbox widget. But if you are using the “values” option too, this option will have no effect.
incrementWith this option, you can decide how many options or numbers should skip when the user clicks the arrow buttons.
buttonbackgroundThis option is used to specify the background color of the arrow buttons.
valuesYou must specify a tuple or a list of values to this option that you want to show in the Spinbox widget.
foregroundWhen you specify a color name as a value to this option, the color of arrow signs of the arrow buttons and the color of the Spinbox text will get changed.
stateBy using this option, you can change the state of the Spinbox widget. The values should be – “normal”, “readonly” and “disabled”. For example, suppose you specify the value of this option as “readonly”, you cannot type the text inside the Spinbox widget. Hence, you have to choose a value from the list of fixed values.
commandThis option will help you to associate a function that will invoke when the user clicks on the arrow buttons.
Below are some other standard options that you can use with the Spinbox widget are :
  • activebackground
  • background
  • borderwidth
  • cursor
  • font
  • foreground
  • highlightbackground
  • highlightcolor
  • hightlightthickness
  • insertbackground
  • insertborderwidth
  • insertofftime
  • insertontime
  • insertwidth
  • justify
  • relief
  • selectbackground
  • selectborderwidth
  • selectforeground
  • takefocus
  • textvariable
  • xscrollcommand

If you want to know more about these above options see Tkinter Standard Options

Example:
from tkinter import *
root=Tk()

# spinbox1 is showing the use of from, to, background, font, buttonbackground, width
spinbox1=Spinbox(root,from_=2,to=50,background='yellow',font="Arial 25 bold",buttonbackground='lightgreen',width=20,increment=2)
spinbox1.pack()

# spinbox2 is showing the use of from, to, background, font, buttonbackground, width, state, readonlybackground, foreground
spinbox2=Spinbox(root,from_=1,to=50,background='skyblue',font="Arial 25 bold",buttonbackground='orange',width=20,state='readonly',readonlybackground='red',foreground='white')
spinbox2.pack()

# spinbox3 is showing the use of from, to, buttonbackground, font, disabledbackground, width, state, foreground
spinbox3=Spinbox(root,from_=1,to=50,buttonbackground='orange',font="Arial 25 bold",width=20,state='disabled',disabledbackground='lightblue',foreground='red')
spinbox3.pack()

def displayday():
    # get method is used to get the value of spinbox4
    val=spinbox4.get()
    lab=Label(root,text=val)
    lab.pack()

# this is list of values for spinbox4
daylist=('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')

# spinbox4 is showing the use of buttonbackground, font, readonlybackground, width, state, foreground, values
spinbox4=Spinbox(root,font="Arial 25 bold",buttonbackground='red',width=20,state='readonly',readonlybackground='green',foreground='white',values=daylist, command=displayday)
spinbox4.pack()

root.mainloop()
The output will be look like this:
This image showing the use of different Tkinter Spinboxes and the use of its configuration options
Below is the list of some methods of the Entry widget that is also supported by the Tkinter Spinbox widget:
  • delete()
  • icursor()
  • index()
  • insert()
  • selection_get()
  • selection_clear()
  • get()
Example:
from tkinter import *
root=Tk()

# these are functions which are showing the use of spinbox widget methods

def deleteselection():
    spinbox.delete(first='sel.first',last='sel.last')

def setcursor():
    spinbox.icursor(1)

def getindexnumber():
    indexnumber=spinbox.index(index='end')
    label=Label(root,text=indexnumber)
    label.pack()

def invokebuttonup():
    spinbox.invoke('buttonup')

def insertext():
    spinbox.insert('end',' new text ')

def getselectedtext():
    val=spinbox.selection_get()
    lab=Label(root, text=f'{val}')
    lab.pack()

def clearselection():
    spinbox.selection_clear()

def getext():
    val=spinbox.get()
    lab=Label(root, text=f'{val}')
    lab.pack()


# this is list of values for spinbox4
daylist=('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')

# spinbox is showing the use of buttonbackground, font, background, width,  foreground, values
spinbox=Spinbox(root,font="Arial 25 bold",buttonbackground='red',width=20,background='lightgreen',foreground='white',values=daylist,command=getext)
spinbox.pack()

# btn to invoke upper button function  
clickupperbtn=Button(root,text='click upper button',command=invokebuttonup)
clickupperbtn.pack()

# btn to delete selected text
deleteselectedbtn=Button(root,text='delete selection',command=deleteselection)
deleteselectedbtn.pack()

# btn to get the index number of last character
getindexpositionbtn=Button(root,text='get index number of last selected character',command=getindexnumber)
getindexpositionbtn.pack()

# btn to set cursor before first character
setcursorbtn=Button(root,text='set cursor to first character',command=setcursor)
setcursorbtn.pack()

# btn to insert text inside spinbox
inserttextbtn=Button(root,text='insert new text',command=insertext)
inserttextbtn.pack()

# btn to clear selection
clearselectionbtn=Button(root,text='clear selection',command=clearselection)
clearselectionbtn.pack()

# btn to get selected text
getselectedtextbtn=Button(root,text='get selected text',command=getselectedtext)
getselectedtextbtn.pack()

root.mainloop()
The output will be look like this:
This image showing the use of different Tkinter Spinboxe commands

Similar Posts

Leave a Reply

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