下面的脚本要求用户输入并打开一个窗口。然后我想从窗口中获取信息并将它们放在列表或类似列表中,以创建“参数”类的对象。一切正常,除非我尝试使用 Entry.get 提取数据以将它们放入列表列表中。
class Parameter (object):
def __init__(self,number_individuals_classes,payoff):
self.nb_classes = number_individuals_classes
self.payoff = payoff
def __repr__(self):
print('nc.classes: {} | payoff: {}'.format(self.nb_classes,self.payoff))
def get_parameters ():
def get_payoff():
global payoff
payoff = []
for i in xrange(len(entr)):
payoff.append(map(Entry.get, entr[i]))
fen1.destroy()
number_individual_classes = input('Number of individual classes: ')
fen1 = Tk()
fen1.title('Enter payoff matrices')
header1 = Label(fen1,text='Cooperation').grid(row=0,column=2)
header2 = Label(fen1,text='Defection').grid(row=0,column=3)
other_txts = []
focal_txts = []
vert_cop_def_txts = []
entr = []
iteration = 0
for focal in range(1,number_individual_classes):
for other in range(focal+1,number_individual_classes+1):
focal_txts.append(Label(fen1,text='Effect on: {}'.format(focal)).grid(column=0,row=3*iteration+2))
vert_cop_def_txts.append((Label(fen1,text='Cooperation').grid(column=1,row=3*iteration+2),Label(fen1,text='Defection').grid(column=1,row=3*iteration+3)))
other_txts.append(Label(fen1,text=' '*65 +'Co-player: {}'.format(other)).grid(row=3*iteration+1,column=2))
entr.append((Entry(fen1).grid(row=iteration*3+2,column=2),Entry(fen1).grid(row=iteration*3+2,column=3),Entry(fen1).grid(row=iteration*3+3,column=2),Entry(fen1).grid(row=iteration*3+3,column=3)))
iteration+=1
Button(fen1,text='Done',command=get_payoff).grid()
fen1.mainloop()
to_return = Parameter(number_individual_classes,payoff)
return to_return
a=get_parameters()
我究竟做错了什么 ?
更新:
这项工作。有什么区别 ?
from Tkinter import *
def super_function():
out = map(Entry.get, entr)
fen1.destroy()
print out
fen1 = Tk()
entr = []
for i in xrange(10):
entr.append(Entry(fen1))
entr[i].grid(row=i+1)
Button(fen1, text = 'store everything in a list', command = super_function).grid()
fen1.mainloop()
Ps 由于我是初学者,欢迎对我的脚本提出任何其他建议 :)