I am using Python 2.7 and Tkinter to make a GUI for my code. At one point, a frame is filled with many buttons in a loop. When I click on one of the buttons, the function needs to know from where it was called, so I googled and found out this nice way to do it:
def generate_buttons(n):
for i in xrange(n):
newbutton = str(i)
newbutton = Button(myFrame, text=newbutton, command= lambda name=i:print_name(name)).grid(column=i)
and:
def print_name(name):
print name
So when I generate my buttons like this:
generate_buttons(5)
the 5 buttons appear in one row. If I click on number 3, console prints "3".
Now what do I do if I want to further access the buttons. For example give them a new appearence. If I created just one button, it would be easy:
myButton.Button(text="bla")
myButton.config(relief=SUNKEN)
or
myButton.config(padx=10)
But in the loop, I override "newbutton" all the time. Does that mean I cannot get access to the individuals anymore? How do I "address" an object that was iteratively created and thus does not have a name?