Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to make it so each button, will display a "*" when you click on it and switch between player 1 and 2. But each time it click it returns the last button in the game and puts a "*" there.

Python
# create buttons

button_dict = {}

for y_pos in range(7):
    for x_pos in range(6):
        def change_player(obj, button):
            player_select.config(text="Player_2_Turn")
            p2 = True
            theButton = ""
            for key in button:
                theButton = key
            button[theButton].config(text=obj)
        
     # create the buttons 
        button_dict[y_pos] = Button(
            frame, 
            text = '', 
            width=16, 
            height=7, 
            bg="Black",
            command = lambda
            text = "*", : change_player(text, frame.children),
            fg="Light Blue",
        )
        button_dict[y_pos].grid(row=x_pos, column=y_pos)


Is there a better way to do this that works?

What I have tried:

I've tried different/similar ways to complete the same problem but I have the feeling i am going about this completely wrong, I would appreciate some help if anybody comes across this question.
Posted
Updated 7-Apr-23 12:16pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900