Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter Spinbox Widget Setting Default Value

Tags:

tkinter

I have a tkinter spinbox:

sb = Spinbox(frame, from_=1, to=12)

I would like to set the default value of spinbox to 4. How do i do this ?

i have read this thread where Bryan suggests setting

Tkinter.Spinbox(values=(1,2,3,4))
sb.delete(0,"end")
sb.insert(0,2)

But i did not get the logic behind it.

What has delete and insert to do with setting default values ?

Any further insight would be appreciated.

thanks

like image 934
bhaskarc Avatar asked May 11 '13 17:05

bhaskarc


2 Answers

sb.delete(0,"end") is used to remove all the text from the Spinbox, and with sb.insert(0,2) you insert the number 2 as the new value.

You can also set a default value with the textvariable option:

var = StringVar(root)
var.set("4")
sb = Spinbox(root, from_=1, to=12, textvariable=var)
like image 193
A. Rodas Avatar answered Sep 20 '22 18:09

A. Rodas


As shortcut you could use:

var = tk.DoubleVar(value=2)  # initial value
spinbox = tk.Spinbox(root, from_=1, to=12, textvariable=var)

Optional you may make the value readonly:

spinbox = tk.Spinbox(root, from_=1, to=12, textvariable=var, state='readonly')

like image 42
tammoj Avatar answered Sep 22 '22 18:09

tammoj