Python Simple turtle graphic programme, what's wrong?

  • Thread starter Thread starter ChloeYip
  • Start date Start date
  • Tags Tags
    Graphic
Click For Summary
The discussion revolves around a Python program using the turtle graphics library, where the user aims to make a turtle disappear upon clicking it. The initial code provided does not produce any visible effect when the turtle is clicked, despite not generating error messages. Key points include the need for the function assigned to `turtle.onclick()` to accept two parameters representing the click coordinates, even if they are not used within the function. The corrected code includes defining the `hide` function with parameters `(x, y)`, which allows the turtle to respond to clicks. Additionally, participants suggest consulting the turtle graphics documentation for further guidance and examples.
ChloeYip
Messages
93
Reaction score
1
I want to write a program when I click the turtle, the turtle would disappear.
However, the program does not gave error message but when I click the turtle, there is no effect on it.
What's wrong with it?
How can I fix it?
Thanks
Mod note: Added code tags.
My written programme:
Python:
import turtle
import pygame

def hide():
    turtle.hideturtle()
turtle.onclick(hide)
 
Last edited by a moderator:
Technology news on Phys.org
ChloeYip said:
I want to write a program when I click the turtle, the turtle would disappear.
However, the program does not gave error message but when I click the turtle, there is no effect on it.
What's wrong with it?
How can I fix it?
Thanks
Mod note: Added code tags.
My written programme:
Python:
import turtle
import pygame

def hide():
    turtle.hideturtle()
turtle.onclick(hide)
Please use code tags, especially with Python code!
They look like this
Python:
<your code>
I don't know anything about turtle graphics in Python, but I suspect if you want to hide the turtle, you first have to make it appear.Have you looked at any documentation for turtle graphics in Python? I did a web search for "python turtle" and found this as the first link: https://docs.python.org/2/library/turtle.html
 
ChloeYip said:
I want to write a program when I click the turtle, the turtle would disappear.
However, the program does not gave error message but when I click the turtle, there is no effect on it.
What's wrong with it?
How can I fix it?
Thanks
Mod note: Added code tags.
My written programme:
Python:
import turtle
import pygame

def hide():
    turtle.hideturtle()
turtle.onclick(hide)

According to the documentation the function which is given as argument to onclick should take two arguments representing the coordinates.
 
  • Like
Likes jedishrfu
jedishrfu said:
Here's a blog on turtle graphics

https://michael0x2a.com/blog/turtle-examples

Notice the very first thing is to create a turtle via turtle.Turtle()
There is already one turtle when import turtle. I don't think it is a problem if I do not create one more...

eys_physics said:
According to the documentation the function which is given as argument to onclick should take two arguments representing the coordinates.

Do you mean I should add a line like "x,y = turtle.pos()"? But I thought "turtle.onclick()" is meaning to click the turtle...
 
No, you don't need. But, your function hide should take to parameters (the coordinates). However, You don't need to do something with these parameters inside the function.
 
Python:
import turtle 
import pygame 
def hide(x,y):     
     turtle.hideturtle() 
turtle.onclick(hide)

Do you mean like this?
 
ChloeYip said:
Python:
import turtle
import pygame
def hide(x,y):    
     turtle.hideturtle()
turtle.onclick(hide)

Do you mean like this?

Yes. Does it help?
 
The turtle doesn't response when i click on it...
 
  • #10
ChloeYip said:
The turtle doesn't response when i click on it...
Do you have any more code than what you show in this thread?

All you're doing in the code here is hiding the turtle. Have you looked at the documentation for turtle graphics? If not, follow this link: https://docs.python.org/3/library/frameworks.html
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
2K
Replies
55
Views
6K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K