【发布时间】:2020-11-27 22:35:47
【问题描述】:
我正在编写一个使用网格几何管理创建框架的程序。在那个框架中,我用一个循环创建了 5 行和 7 列。在每一行中,我有 5 列条目和两列复选按钮。每个条目和复选框都分配给一个变量,并且该变量使用网格几何管理中的(行,列)作为键存储在字典中。
widgets = {}
widgetsValue = {}
for i in range(rows): #Rows
for j in range(columns): #Columns
if j == 2 or j == 3: #column 2 and 3 is a checkbox
test = IntVar()
c = Checkbutton(inputFrame, bd=1, variable=test)
c.grid(row=2+i, column=j)
widgets[(i, j)] = c
widgetsValue[(i,j)] = test
else: # everything other than column 2 and 3 is a entry
test1 = StringVar()
e = Entry(inputFrame, text="", textvariable=test1)
e.grid(row=2+i, column=j)
widgets[(i, j)] = e
widgetsValue[(i,j)] = test1
现在我无法创建一个按钮,一旦单击该按钮,它将以“第 1 行:”entryvalue、entryvalue、checkbuttonvalue、checkbuttonvalue、entryvalue、entryvalue 的形式使用每行的值更新标签, 第 2 行:" 等等。
这是我的想法。
def submit():
global mystr
for i in range(rows): #Rows
mystr += "row[" + i + "]: "
for j in range(columns):
if (i,j) in widgets:
if widgets[(i,j)].winfo_class() == Entry:
if len(widgets[(i,j)].get()) != 0 :
mystr += widgets[(i,j)].get() + ", "
if widgets[(i,j)].winfo_class() == Checkbutton:
mystr += str(widgetsValue[(i,j)]) + ", "
myArr.append(mystr)
for x in myArr:
mystr += x
hiddenLabel['text'] = mystr # update hiddenlabel with mystr
enter code here
【问题讨论】:
-
我不清楚你的问题是什么。另外,看看你的想法,为什么你必须检查小部件是否是
Entry?为什么你不能直接得到StringVar.get/IntVar.get?