Tkinter create buttons iteratively, but be able to configure specific buttons
I need to create 100 buttons in a 10x10 grid. I've done this, and managed to find which button is being pressed using lambda and sending a values along with it (x and y), as shown below.
def button_pressed(buttonx, buttony):
print(buttonx, buttony)
if self.currentNonogram[buttony][buttonx] == 1:
print("correct")
else:
print("incorrect")
# Buttons
for x in range(1, 11):
tk.Label(text=self.rows[x-1]).grid(row=x+4, column=2)
for y in range(1, 11):
self.labelText = str(self.columns[y-1])
self.labelText = self.labelText.replace("[", "")
self.labelText = self.labelText.replace("]", "")
self.labelText = self.labelText.replace(",", "")
self.labelText = self.labelText.replace(" ", "\n")
tk.Label(text=self.labelText).grid(row=3, column=y+2)
tk.Button(width="1", height="1", command = lambda y=y,x=x:button_pressed(x-1, y-1)).grid(row=x+4, column=y+2)
In the function which is called when the buttons are pressed, where it prints "correct" I want to change the background colour of the buttons to black, but I can't figure out a way to configure a specific one of these buttons like this, as usually this would be done by the name, and clearly I don't want to have to create separate names for each button.
Thanks!
Comments
Post a Comment