I am new to Python's tkinter module so I would be grateful for any help, even an explanation as to why this is simply not possible (if that is the case).
I have a list of 4-tuples of the form (foo, bar, x, y); something like this:
TUPLE_LIST = [('Hello', 'World', 0, 0),
('Hovercraft', 'Eels', 50, 100),
etc.]
and a for loop later which ideally instantiates the variable foo as a button with the text bar, with each button having a function to add its respective bar to an already defined Entry widget, then places it at the co-ordinates x, y:
for foo, bar, x, y in TUPLE_LIST:
exec("{0} = Button(self, text='{1}', command=lambda: self.update_text('{1}'))".format(foo, bar))
eval(name).place(x=x, y=y)
The buttons place perfectly, but if I go to click on one of the them, I get the following error:
Traceback (most recent call last):
File "...\lib\tkinter\__init__.py", line 1550, in __call__
return self.func(*args)
File "<string>", line 1, in <lambda>
NameError: name 'self' is not defined
I'm guessing this has something to do with the fact that the command is defined with a lambda, and thus isn't a defined 'function', per se, and yet I have seen other people define their buttons' commands with lambdas. So is it something to do with using exec as well? Also, here's the code for update_text (if it matters):
def update_text(self, new_char):
old_str = self.text_box_str.get()
# Above is a StringVar() defined in __init__ and used as textvariable in Entry widget creation.
self.text_box_str.set(old_str + new_char)
Any help would be much appreciated. Thanks in advance.