【发布时间】:2012-06-29 13:21:06
【问题描述】:
首先为代码的长度道歉,但我想把它全部展示出来。
我的界面如下所示:
当我将第三个选项菜单更改为“列表”时,我将添加具有“n”值的选项(未显示)。然后我需要'n'列,用户可以在其中输入他们的值。
我还有一个问题,根据界面打开的文本文件,可能会有“n”行。
因此,我想知道是否有可能(因为我很难在框中不重复相同的值,现在我需要“n”列)添加“n”个行和列,正如我的代码显示的那样,只需添加4列。我可以读取行数,但无法返回所有这些值,具体取决于有多少行。到目前为止,我可以做一排..
谢谢!
def numberwritten(number):
fg = number.get()
print fg
numbers = [StringVar() for i in xrange(4) ] #Name available in global scope. Need to add other rows?
for i in numbers:
i.trace('w',lambda a,b,c,n=i: numberwritten(n) )
def ChoiceBox(choice):
co_ord = str(frame_table.grid_size())
col, rows = map(float, co_ord.strip('()').split(','))
rows = int(rows)
if choice == "Fixed":
empty1.destroy()
#choice_frame.grid_forget()
tkMessageBox.showinfo("Message", "No optimisation, value fixed.")
elif choice == "List":
column = 7
for i in xrange(4):
choice_title = Label(frame_table, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=10)
choice_title.grid(row=1, column=column+i, sticky="nsew", padx=1, pady=1)
boxes=[]
for i in xrange(4):
for j in range(2, rows):
box=Entry(frame_table,bg='white',borderwidth=0,textvariable=numbers[i], width=10, justify="center") # Here I'm having problems with rows
box.grid(row=j,column=column+i, sticky='nsew', padx=1, pady=1)
boxes.append(box)
box1,box2,box3,box4=boxes
elif choice == "Interval" or "Optimisation":
for i in xrange(2):
choice_title1 = Label(frame_table, text='Min Value', bg='white', borderwidth=0)
choice_title1.grid(row=0, column=column, sticky="nsew", padx=1, pady=1)
choice_title2 = Label(frame_table, text='Max Value', bg='white', borderwidth=0)
choice_title2.grid(row=0, column=column+1, sticky="nsew", padx=1, pady=1)
boxes=[]
for i in xrange(2):
box=Entry(frame_table,bg='white',borderwidth=0,textvariable=numbers[i])
box.grid(row=rows+1,column=i, sticky='ew', padx=1, pady=1)
boxes.append(box)
box1,box2,box3,box4=boxes
更新:我稍微进步了,ChoiceBox 中的部分现在位于类 Window 下:是 ChoiceBox(self,choice),我有以下部分需要更改注释行以接受“n”数量盒子。
column = 7
for i in xrange(self.number_boxes):
choice_title = Label(self.frame_table, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=10)
choice_title.grid(row=1, column=column+i, sticky="nsew", padx=1, pady=1)
boxes=[]
for i in xrange(self.number_boxes):
for j in range(2, rows):
box=Entry(self.frame_table,bg='white',borderwidth=0,textvariable=numbers[i], width=10, justify="center")
box.grid(row=j,column=column+i, sticky='nsew', padx=1, pady=1)
boxes.append(box)
#box1,box2,box3,box4=boxes
然而,我仍然存在使用 numberwritten (包括原始示例中的前几行代码)来完全提取值列表的问题,最好是针对每一行。
【问题讨论】:
-
当你写'将第二个选项菜单更改为“列表”'时,你真的是指第三个选项菜单(在“选择”列中)吗?另外,您可以添加多少列?你说“n”。 “n”可以是 100 吗?如果“n”不止几个,那么从可用性的角度来看,这个解决方案的扩展性就不是很好。
-
对不起,我的意思是第三个,问题已编辑。 n 不会是一个很大的数字,最多可能是 20 左右。
-
您确实意识到,对于每列非常保守的 100 像素,您正在谈论将 2000 像素添加到表单的宽度。你确定要走这条路吗?
-
我被赋予了根据行数添加这些列的任务,所以是的,这就是我打算做的事情。只是对如何根据否来做到这一点感到非常困惑。行/列,反之亦然。
-
我已经稍微改变了我的答案,以表明我现在需要帮助。这是获取每行值列表的情况,但列数可能是“n”(由用户提供)并且行数 = 行数,因为这可能会根据打开的文本文件而改变。
标签: python user-interface if-statement tkinter ttk