我有以下代码(仅部分代码)用于获取我创建的 4 个条目框中的条目。但是我有两个小问题:
当我在每个框中输入时,它会输入相同的内容,我希望输入不同的数字并将它们全部分配给单独的变量。
有没有办法在一个循环中产生 4 个盒子并且比这更少的行?
number = StringVar() def numberwritten(*args): number.trace("w", numberwritten) fg = number.get() print fg
在单独的定义中 def ChoiceBox(choice): (不是这个 def 下的完整代码)
def 选择框(选择):i = [0, 1, 2, 3]
for i in i: choice_title = Label(choice_frame, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=0) choice_title.grid(row=0, column=column+i, sticky="nsew", padx=1, pady=1) box1 = Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=number) box1.grid(row=1, column=0, sticky="ew", padx=1, pady=1) box2 = Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=number) box2.grid(row=1, column=1, sticky="ew", padx=1, pady=1) box3 = Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=number) box3.grid(row=1, column=2, sticky="ew", padx=1, pady=1) box4 = Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=number) box4.grid(row=1, column=3, sticky="ew", padx=1, pady=1)
更新/编辑:
这是我拥有的代码部分,由于我收到语法错误,因此无法弄清楚它最后出了什么问题:
def numberwritten(number):
fg = number.get()
print fg
numbers = [StringVar() for i in xrange(4) ] #Name available in global scope.
for i in numbers:
i.trace('w',lambda n=i: numberwritten(n) )
def ChoiceBox(choice):
column = 0
if choice == "Fixed":
choice_frame.grid_forget()
tkMessageBox.showinfo("Message", "No optimisation, value fixed.")
elif choice == "List":
for i in xrange(4):
choice_title = Label(choice_frame, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=0)
choice_title.grid(row=0, column=column+i, sticky="nsew", padx=1, pady=1)
boxes=[]
tkMessageBox.showinfo("Message", "Please fill in list values.")
elif choice == "Interval" or "Optimisation":
i = [0, 1]
choice_title1 = Label(choice_frame, text='Min Value', bg='white', borderwidth=0, width=0)
choice_title1.grid(row=0, column=column, sticky="N S E W", padx=1, pady=1)
choice_title2 = Label(choice_frame, text='Max Value', bg='white', borderwidth=0, width=0)
choice_title2.grid(row=0, column=column+1, sticky="nsew", padx=1, pady=1)
boxes=[]
tkMessageBox.showinfo("Message", "Enter Min/Max values.")
for i in xrange(4):
box=Entry(choice_frame,bg='white',borderwidth=0,textvariable=numbers[i])
box.grid(row=1,column=i, sticky='ew', padx=1, pady=1
boxes.append(box)
box1,box2,box3,box4=boxes