您正在创建小部件的行。每行可能具有比其他行更多或更少的小部件。在某些时候,你需要得到一个代表每行数据的列表。你在问,“我怎么得到这个清单?”。我对么?
您一定要问20个关于这个简单问题的问题。问题不在于此或任何其他单一功能,而在于您的一般架构。一位非常非常聪明的程序员曾告诉我“如果你想让我理解你的代码,不要告诉我你的代码,让我看看你的数据结构”。这里的根本问题是,你没有数据结构。如果你不组织你的数据,你不能希望轻松获取数据。
这个问题没有什么难的。保留每行的条目窗口小部件列表,并在需要值时迭代该列表。下面的伪代码显示了这可能是多么的简单:
class MyApp(object):
def __init__(self):
# this represents your data model. Each item in the
# dict will represent one row, with the key being the
# row number
self.model = {}
def add_row(self, parent, row_number):
'''Create a new row'''
e1 = Entry(parent, ...)
e2 = Entry(parent, ...)
e3 = Entry(parent, ...)
...
# save the widgets to our model
self.model[row_number] = [e1, e2, e3]
def extend_row(self, parent, row_number, n):
'''Add additional widgets to a row'''
for i in range(n):
e = Entry(parent, ...)
self.model[row_number].append(e)
def get_values(row_number):
'''Return the values in the widgets for a row, in order'''
result = [widget.get() for widget in self.model[row_number]]
return result
在我看来,你的格式化字符串是错误的,因为参数的数量不符合说明符的数量。看看Python文档(http://docs.python.org/library/stdtypes.html#string-formatting-operations)并尝试摆弄你的打印语句。 – 2012-07-12 07:22:08
感谢您的链接,但我似乎无法得到它的工作,因为我需要等待,直到我的所有箱子都被填充.. = S – user2063 2012-07-12 08:37:04
我改变了我的方法,因为我知道另一种方式至少有一个值的工作。 – user2063 2012-07-12 09:14:32