Okay, so I started programming Minesweeper. For that, I need a variable number of Buttons (or should I use something different). So I got two loops which should create a button with a name. But I need the buttons of course to have other names. So f.i.: the first button = button_1 the second button = button_2 ...
My code goes so:
class test(Tk):
def __init__(self):
self.frame_game = Frame(self)
for i in range(1, height):
for j in range(1, height):
# here i need the button to be created
I tried it with:
globals()['Button_' + str(i) + str(j)] = Button(...)
but that doesn't work because I need a button which is global.
I also tried it with
vars(self)['Button_' + str(i) + str(j)] = Button(self.frame_game, text='0').pack()
I pack the label later with:
self.frame_game.pack()
It doesn't throw an Exception.
Anyone got an idea?
EDIT: IT WORKS!!!! The code just didnt go into the loop. changed it to
for i in range(0, height):
Also of course the other loop with j. The working code is in the loop following:
vars(self)['Button_' + str(i) + str(j)] = Button(self.frame_game, text='0').pack()
listordictto host your dynamically created objects instead of dynamically injecting them to yourglobals.