bhaskarc
bhaskarc

Reputation: 9521

Tkinter Spinbox Widget Setting Default Value

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

Upvotes: 17

Views: 34469

Answers (2)

tammoj
tammoj

Reputation: 948

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')

Upvotes: 7

A. Rodas
A. Rodas

Reputation: 20679

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)

Upvotes: 24

Related Questions