Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Isuess with Python turtle library "onclick" function

So, the problem is is that the "onclick" function is not working. It is instead just printing the test code (print(‘working’)) when the code is started. Please help!

SubmitButton = turtle.Turtle()
SubmitButton.penup()
SubmitButton.goto(0,-300)
SubmitButton.shape('square')
SubmitButton.shapesize(2)
SubmitButton.fillcolor('red')
SubmitButton.penup()
SubmitButton.goto
SubmitButton.onclick(print("working"))

There is the snippet of the code, lemme know if you need anymore.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

onclick needs to be passed a function, which it will call. The function needs to have two arguments. (See the Documentation, which you should always look up before posting here). You’re passing it the returned value of a print(), which is None. You could use a lambda, which creates a function that will call print() when that function is called:

SubmitButton.onclick(lambda x, y: print("working"))

Or you could define a function and pass that in:

def onclick_function(x, y):
    print("working")

SubmitButton.onclick(onclick_function)

Notice though how I’m passing in onclick_function but not calling that function.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading