14

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

4

2 回答 2

20

sb.delete(0,"end")用于从 Spinbox 中删除所有文本,并sb.insert(0,2)插入数字 2 作为新值。

您还可以使用以下textvariable选项设置默认值:

var = StringVar(root)
var.set("4")
sb = Spinbox(root, from_=1, to=12, textvariable=var)
于 2013-05-11T17:53:08.333 回答
3

作为快捷方式,您可以使用:

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

可选的,您可以将值设为只读:

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

于 2018-12-17T12:17:30.367 回答