I'm writing a program with a GUI for school.
I'm supposed to write Pah Tum. Its played on a 7x7 matrix. My console game is done and works perfectly, but I'm struggling with Tkinter right now.
So far I have a few frames inside frames defined in my __init__
function.
So now the first problem:
I could make a 7x7 board like this:
for x in range(0, 7):
for y in range(0, 7):
self.button = Button(self.board, command=self.action)
self.button.grid(row = x, column = y)
So what I want to do now is, every time I hit a button I want to change the color to red (for player 1) or blue (if its player 2s turn).
self.turn_tracker = 0 # is in __init__
def action(self):
if self.turn_tracker in range(0, 49, 2):
self.turn_tracker += 1
self.button.configure(bg="blue")
elif self.turn_tracker in range(1, 49, 2):
self.turn_tracker += 1
self.button.configure(bg="red")
elif self.turn_tracker == 49:
print("game over")
sys.exit() #calculate
This will only change the button at 6x6. So what I tried is defining every button separately and having a change method for every button separately as well. As you can imagine this looks really really ugly but it works at least.
What can I do to make it more efficient? Thanks for any help!