Python Tkinter - SpinBox

Pybeginner
By -
0



In Tkinter we can also create a spinBox. To create a Spinbox widget, you can use the Spinbox class like this:

spin = Spinbox(window, from_ = 0, to = 100)

 

Here, we create a Spinbox widget and pass the parameters from_ and to specify the number range for the Spinbox.

In addition, you can specify the width of the widget using the parameter width

spin = Spinbox(window, from_ = 0, to = 100, width = 5)

 

The complete code will be:

from tkinter import *

window = Tk()
window.title ( "SpinBox")
window.geometry('220x200')

spin = Spinbox(window, from_=0, to=100, width=5)
spin.grid(column=0,row=0, padx=10, pady=10)

window.mainloop()

 



 

You can specify the numbers for Spinbox instead of using the entire range like this:

spin = Spinbox(window, values=(3, 8, 11), width=5)

 

Before, the Spinbox widget only shows these 3 numbers just 3 , 8 and 11.

 

Python Tkinter - How to set default value for Spinbox

 

To set the default Spinbox value, you can pass the value to the text variable parameter like this:

 

var =IntVar()
var.set(36)
spin = Spinbox(window, from_=0, to=100, width=5, textvariable=var)

 

Now, if you run the program, it will show 36 as a default value for Spinbox.

 

Post a Comment

0Comments

Post a Comment (0)