Search code examples
pythonimageuser-interfacegridguizero

Changing picture on python guizero grid when clicked


I have been having a terrible problem and would really, really appreciate it that somebody could help me...

Can this be fixed?

So basically, on guizero python I want a button to update a picture when clicked. Here on my battleship game, I have a grid with each picture being a water tile. When clicked on the water tile I want it to be updated so that the it displays a hit water tile.

I have tried to make it so:

def hit():
    button_11.image = "images/shipbroken.GIF"

button_11 = PushButton(window, image="images/watergrid.GIF",  grid=[1, 1], height = 100, width = 100) 

button_11.whenclicked = hit 

This should go to the subroutine "hit" when clicked and update button_11's image to a destroyed tile. I dont know why this is not working and I feel the solution is obvious but out of reach facepalm


Solution

  • Base on documentation and source code PushButton you should use command=hit

    button_11 = PushButton(window, 
                           image="images/watergrid.GIF",  
                           grid=[1, 1], 
                           height=100, 
                           width=100, 
                           command=hit) 
    

    Eventually

    button_11.update_command(hit)