Simple turtle graphic programme, what's wrong?

  • Python
  • Thread starter ChloeYip
  • Start date
  • Tags
    Graphic
In summary: It seems like you may need to create a turtle object, set its visible property to True and then define an onclick event for that object. Please refer to the documentation for more details.
  • #1
ChloeYip
93
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
  • #2
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
[code=python]
<your code>
[/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
 
  • #4
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
  • #5
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...
 
  • #6
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.
 
  • #7
Python:
import turtle 
import pygame 
def hide(x,y):     
     turtle.hideturtle() 
turtle.onclick(hide)

Do you mean like this?
 
  • #8
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?
 
  • #9
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
 

FAQ: Simple turtle graphic programme, what's wrong?

1. What is a simple turtle graphic programme?

A simple turtle graphic programme is a computer program that uses a turtle object to draw lines and shapes on a canvas. The turtle moves forward and turns based on commands given by the user, creating drawings and patterns.

2. How does a turtle graphic programme work?

A turtle graphic programme works by using commands to control the movement and drawing of a turtle object. The turtle starts at a specified location on a canvas, and the user can give commands to move the turtle, change its direction, and draw lines or shapes.

3. What are some common errors in a simple turtle graphic programme?

Some common errors in a simple turtle graphic programme include misspelling commands or using incorrect syntax, not defining the turtle object or canvas, and using invalid or out-of-range values for turtle movements or drawing commands.

4. How can I fix errors in a simple turtle graphic programme?

To fix errors in a simple turtle graphic programme, carefully check the syntax and spelling of commands, make sure the turtle object and canvas are properly defined, and use valid values for commands. It can also be helpful to refer to documentation or tutorials for guidance.

5. What are some tips for using a simple turtle graphic programme?

Some tips for using a simple turtle graphic programme include starting with simple commands and building up to more complex drawings, using loops and functions to create repetitive patterns, and experimenting with different values and commands to see their effects on the drawing. It can also be helpful to save your progress and regularly test your code to catch errors early on.

Similar threads

Replies
2
Views
2K
Replies
9
Views
2K
Replies
4
Views
4K
Replies
3
Views
1K
Back
Top