[Python] How do I exit a while loop in turtle graphics with an event?

Discussão em 'Python' iniciado por Stack, Agosto 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm making this project for school that involves displaying data to a raspberry pi. The code I'm using refreshes (and needs to refresh) incredibly quickly, but I need a way for the user to stop the output, which I believe requires some kind of key event. The thing is, I'm new to Python and I can't figure out how to exit a while loop with turtle.onkey(). I found this code:

    import turtle

    def quit():
    global more
    more = False

    turtle.onkey(quit, "Up")
    turtle.listen()

    more = True
    while more:
    print("something")


    Which doesn't work. I've tested it. How do I make this work, or is there another way to get user input without interrupting the flow of the program?

    Continue reading...

Compartilhe esta Página