Reputation: 18219
The below script asks the user an input and open a window in consequence. Then I want to get the informations from the window and put them in a list of list or something like that in order to create an object of the class "Parameter". Everything works except when I try to extract the data with Entry.get to put them in a list of list.
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()
What am I doing wrong ?
Update:
This work. what is the difference ?
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()
P.s As I am a beginer, any other advice on my script is more than welcome :)
Upvotes: 0
Views: 2168
Reputation: 20679
The first and most important problem is that you are appending to the lists the result of the calls to grid
(which is always None
), instead of the widget:
focal_txts.append(Label(fen1,...).grid(...))
# ...
This should be:
focal_label = Label(fen1,...)
focal_label.grid(...)
focal_txts.append(focal_label)
Besides, you are trying to use payoff
as a global variable, but there is no global name payoff
before you use it in your callback function. So when you try to use it as an argument for the constructor of Parameter
, it is not within the same scope.
In general, the creation of the widgets dinamically can be improved and the organization of your code would be much better if you use classes.
Upvotes: 1
Reputation: 540
I am not really sure what you intend to do with this, it seems like you want to establish some kind of interaction matrix between each "individual class" but I may be wrong.
First, your function doesn't have any argument ( get_payoff() ), since you want it to extract something from entr, I would assume that you would want to put entr as an argument of your function! Something like :
Button(fen1,text='Done', command=lambda : get_payoff(entr)).grid()
"lambda" will allow the function to be used without being called when the button is initially created in the GUI.
Second, when I execute (with the modification), I've got an error because in the function you will try to do the get() with None types variables. Your variable "entr" where you want to extract the data contains only None types, not textvariables from Entry widget. The reason is that you can't store widgets in an array like this. Each time you want to create an entry, you must create a variable (a textvariable) which will be the link to the entry:
# a is a string variable
a = StrVar()
# which is linked to the Entry "test"
test = Entry(window, textvariable = a)
# get the variable in Entry "test"
b = test.get()
# print on screen the result
print b
I don't know if that can help you or if I am completely off the mark here.
Upvotes: 2