Tkinter widget with default setting

I have a tkinter spinbox:

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

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

I read this topic where Brian suggests installing

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

But I did not have logic.

What to remove and insert to set default values?

Any further understanding will be appreciated.

thanks

+7
source share
2 answers

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

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

 var = StringVar(root) var.set("4") sb = Spinbox(root, from_=1, to=12, textvariable=var) 
+13
source

As a shortcut you can use:

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

Optionally, you can make the value read-only:

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

0
source

Source: https://habr.com/ru/post/944778/


All Articles