Python using turtle button

12,758

So after extensive search there isn't necessarily a way pause execution of the code in python while using turtle to wait for some click event. Maybe in Tk I could do that but not in turtle.

However, there is a way to get around that. As an example. A method sets up the fake button on the screen, sets the click event, and terminates. The click event when clicked calls the next method needed for execution. So until the button is clicked the actual code isn't doing anything but remains in memory for use.

So more specifically. 1. Create a 'button'. 2. Have your program behave normally until it needs to wait for a click event. 3. Set up the on screen click (or on turtle) in such a way when the 'button' is clicked the next part of the code is run.

Special note. The code in question can't depend on waiting for a click event for later on in code. Instead, the click causes the next part of the execution of your code.

Share:
12,758
user2309351
Author by

user2309351

Updated on June 26, 2022

Comments

  • user2309351
    user2309351 almost 2 years

    I am attempting for a homework assignment to implement Simon Says in python. I'm trying to do it using the turtle library (a requirement).

    However, I've run into a stumbling block in that while I can get the screen to register click events (currently just printing the x,y coordinates) I can't get it to wait for a click event.

    Specifically what I'm planning on doing is having areas on the screen that when they click within that location it is considered as if they had clicked a button. Screen clears and game does whatever.

    However, in experiments in trying to get a working 'button' all that it does is set it so it prints the x,y coordinates but the rest of the program finishes. Didn't wait for the user to click anything. I tried a blocking method of...

    while clicked == False:
        pass
    

    or

    while clicked == False:
        time.sleep(1)
    

    but both methods hangs the program until I manually interrupt and then it'll print the clicks.

    Am I missing an option somewhere?