[Python] How can I make an image pop up on click? (Using Turtle module)?

Discussão em 'Python' iniciado por Stack, Setembro 14, 2025.

  1. Stack

    Stack Membro Participativo

    So what I'm trying to do is make a penny pop up on the screen after I click the 'start game' button. I tried to make a boolean that was set to false, but would be set to true after the start game button was clicked. First, here's the function to insert the penny image, and the start game button.

    import turtle
    wn = turtle.Screen()
    gameStart = False
    wn.setup(500, 500)
    wn.bgcolor('yellow')
    def penny():
    penny = turtle.Turtle()
    penny.shape('circle')


    def startClicked(x,y):
    if -500 < x < 500 and -500 < y < 500:
    wn.clear()
    wn.bgcolor("blue")
    gameStart = True
    else:
    gamestart = False

    wn.onscreenclick(startClicked)
    if gameStart:
    penny()
    else:
    print("No penny")


    wn.mainloop()



    and then I made an if statement that would make the penny appear if gameStart was set to true

    But when I call it in to the main code and run it, after I click the start button, the penny doesn't pop up. How do I make it so that the penny would pop up after clicking the start button?

    Edit: Found the problem! All I had to do was take the if statement

    if gameStart:
    penny()
    else:
    print("No penny")


    and put it in the function

    def startClicked(x,y):
    if -500 < x < 500 and -500 < y < 500:
    wn.clear()
    wn.bgcolor("blue")
    gameStart = True
    else:
    gameStart = False
    if gameStart:
    penny()
    else:
    print("No penny")

    wn.onscreenclick(startClicked)
    wn.mainloop()

    Continue reading...

Compartilhe esta Página