I'm trying to make a very simple Othello iteration with Tkinter and Python and have an idea however I don't know a method to retrieve which button is pressed (through an integer?). I have a made a grid of buttons using
for x in range(8):
for y in range(8):
btn = Button(frame)
buttons.append(btn)
btn.grid(column=x, row=y, sticky=N+S+E+W)
btn.config(bg='green2')
I'm planning to config buttons on press and check all 8 directions by adding and subtracting the values of the buttons to find the button the the left (-8), upper right (+7), etc. I'm very new to coding and would like any feedback, thank you.
Welcome to SO!
You can create a bind on any widget in tkinter, the syntax is:
So for your example you could create a bind for each button and pass the x and y values are arguments to keep track of which button is which in the target function. Something like this:
This will print the coordinate of each button in the grid, you can then replace the print statement with whatever function you'd like.
A list of all binds can be found here