问题描述
我已经阅读了一个很好的解决方案,用于在您使用 from_ to 选项时在 Tkinter Spinbox 小部件上建立默认数值.但是我还没有看到一个可以帮助从元组中建立值(数字或字符串)的方法.
I have read a good solution for establishing a default numerical value on a Tkinter Spinbox widget when you use the from_ to options. But I have not seen a single one that can help establish a value (numerical or string) from a tuple.
我的代码如下,旨在通过元组在 Tkinter Spinbox 小部件中设置默认值:
My code is as follows, and is intended to set a default value in a Tkinter Spinbox widget from a tuple:
from Tkinter import *
root = Tk()
root.geometry('200x200+50+50')
root.title('Prueba')
t = ('One', 'Two', 'Three', 'Four', 'Five')
v = t[3]
var = StringVar()
var.set(v)
sb = Spinbox(root, values=t, textvariable=var, width=10)
sb.place(x=5, y=15)
root.mainloop()
我尝试过的另一个变体如下:
Another variant that I have tried is the following:
from Tkinter import *
root = Tk()
root.geometry('200x200+50+50')
root.title('Prueba')
var = StringVar()
var.set('Four')
sb = Spinbox(root, ('One', 'Two', 'Three', 'Four', 'Five'), textvariable=var, width=10)
sb.place(x=5, y=15)
root.mainloop()
set 方法在 Spinbox 上工作的唯一方法(我从这里获取)如下,并且只适用于数字和在 Spinbox 小部件中建立的选项范围内:
The only way the set method works on Spinbox (and which I took from here) is the following and only works with numbers and within a range established as options in the Spinbox widget:
from Tkinter import *
root = Tk()
root.geometry('200x200+50+50')
root.title('Prueba')
var = StringVar()
var.set(4)
sb = Spinbox(root, from_=1, to=5, textvariable=var, width=10)
sb.place(x=5, y=15)
root.mainloop()
谁能帮我找出如何从元组中在 Tkinter Spinbox 中建立默认值?我将不胜感激!
Can please anyone help me to find out how to establish a default value in a Tkinter Spinbox from a Tuple? I will appreciate that greatly!
推荐答案
如果将 var.set(v)
行移动到 after 创建小部件,则将设置默认值.
If you move the line var.set(v)
to be after creating the widget, the default value will be set.
var = StringVar()
sb = Spinbox(root, values=t, textvariable=var, width=10)
var.set(v)
这篇关于如何在 Tkinter Spinbox 上建立默认字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!