使用 for 循环在 Tkinter 中创建和使用小部件(缩放)
Creating and using widgets (Scale) in Tkinter using for-loop
我目前面临在 Tkinter 中创建许多(超过 20 个)比例尺的问题,自然我尽量不创建和使用它们 "manually"。
创建工作正常:
for ii in range(0,25):
nam='input_a' + str(ii)
nam = Scale(master, from_=100, to=0, orient=VERTICAL)
nam.grid(row=0, column=2+ii)
当我尝试获取值时出现问题:
import numpy as np
def Aux():
a=np.zeros(25)
for ii in range(0,25):
nam='input_a'+str(ii)
a[ii]=nam.get()
return a
问题:nam
仍然是一个 str 对象,因此它不能有属性 get
。
有什么提示吗?谢谢!
将对您的天平的引用保存在列表中
nam = []
for ii in range(0,25):
nam.append(Scale(master, from_=100, to=0, orient=VERTICAL))
nam[-1].grid(row=0, column=2+ii)
然后您可以使用 nam[ii].get()
我发现字典对于存储小部件非常方便,但如果您只想通过整数索引访问它们,列表也能起到同样的作用:
scales = {}
for ii in range(0,25):
scales[ii] = Scale(...)
我目前面临在 Tkinter 中创建许多(超过 20 个)比例尺的问题,自然我尽量不创建和使用它们 "manually"。
创建工作正常:
for ii in range(0,25):
nam='input_a' + str(ii)
nam = Scale(master, from_=100, to=0, orient=VERTICAL)
nam.grid(row=0, column=2+ii)
当我尝试获取值时出现问题:
import numpy as np
def Aux():
a=np.zeros(25)
for ii in range(0,25):
nam='input_a'+str(ii)
a[ii]=nam.get()
return a
问题:nam
仍然是一个 str 对象,因此它不能有属性 get
。
有什么提示吗?谢谢!
将对您的天平的引用保存在列表中
nam = []
for ii in range(0,25):
nam.append(Scale(master, from_=100, to=0, orient=VERTICAL))
nam[-1].grid(row=0, column=2+ii)
然后您可以使用 nam[ii].get()
我发现字典对于存储小部件非常方便,但如果您只想通过整数索引访问它们,列表也能起到同样的作用:
scales = {}
for ii in range(0,25):
scales[ii] = Scale(...)