Nostradamus
Nostradamus

Reputation: 21

Consecutive onscreenclick events in Python turtle graphics

My aim is to call a series of functions (consecutively), each with two arguments, upon left-clicking the screen/canvas, automatically supplied with the coordinates of the point clicked.

My goal is for these to be sequential, not all at once with one click. So, click the screen, call function 1, click the screen again (with new coordinates), call function 2 with new coordinates supplied etc.

Here's what I have tried:

from turtle import *

def f(x, y):
     goto(x, -y) 

def g(x, y):
     goto(-x, y)

def main():
     onscreenclick(f)
     onscreenclick(g)

main()

After reading through the literature on 'onclick', 'onscreenclick', I gather it is related to whether you put 'True','False' or 'None' for the third argument.

After trying various combinations of these, all it does is either call all of them upon first click or just call the last one.

If anyone knows of anywhere I can find a more detailed explanation of mouse click events in Python, especially with Turtle graphics I would be most grateful. Or if you can answer the question yourself of course.

Upvotes: 2

Views: 672

Answers (1)

cdlane
cdlane

Reputation: 41872

If you want different functions on each click, try the following:

from turtle import *

def f(x, y):
    goto(x, -y) 
    onscreenclick(g)  # what happens next time

def g(x, y):
    goto(-x, y)
    onscreenclick(f)  # what happens next time

def main():
    onscreenclick(f)

main()

mainloop()

You should be able to repeatedly click and see the behavior alternating between the two functions.

Your understanding of the third argument to onscreenclick() was correct, you can either replace existing event handling or you can augment it, but it's not set up to alternate it.

Upvotes: 3

Related Questions