As part of a project I'm making a GUI in tkinter for connect4 which allows people to play playervsplayer, playervsAI etc. As part of this, I have a method (choose_move) that is inherited from an interface which returns a value to represent the column that AI or player has chosen. In the instance where it is the player that chooses the move, It needs to return the value associated with one of the seven buttons that will be clicked after the player has thought about their move. Here is how I think the code should look but I'm not sure as I think I may have "coded myself into a corner"
def choose_move():
"""Returns a value between 0 and 6 based on player input."""
# makes buttons visible:
create_move_button_array()
# needs to somehow wait until one of the buttons is clicked
return self.chosen_move
def create_move_button_array():
self.move_buttons = []
for i in range(7):
#sizing, placement etc. of buttons ommited
self.move_buttons.append(tk.Button(command=create_choose_move_action(i)))
def create_choose_move_action(move):
return lambda: self.chosen_move=move
I'm relatively new to programming so apologies for the seemingly dumb question and thanks in advance for any replies!
I realise that this thread has been marked as a duplicate of another one, however the other thread seems to just be discussing the use of the lambda keyword to create function objects to pass as an argument to the button constructor. Could someone explain to me how I can use this to solve the problem I am asking, as I am struggling to connect the dots?