Clear an image from turtle graphics in Python? - python

I wrote the following code:
from turtle import *
ts = Screen(); tu = Turtle()
image = "apple.gif"
ts.addshape(image)
tu.shape(image) # The image is displayed
sleep(2); tu.clear # This doesn't clear the image
done()
tu.clear() doesn't clear the image, although it it belongs to tu turtle. If I use ts.clear(), I clear the screen, i.e. the graphics are cleared, but also all events up to this point (key and mouse) are cleared! (I have not included the events here to keep the code simple, but they are simple key events that have been tested and work fine.)
Is there a way to clear an image -- as one does with drawings -- w/o also clearing events set?

I believe what you're describing is hideturtle(). For example:
from time import sleep
from turtle import Turtle, Screen
IMAGE = "apple.gif"
screen = Screen()
screen.addshape(IMAGE)
turtle = Turtle()
turtle.shape(IMAGE) # The image is displayed
sleep(2)
turtle.hideturtle()
screen.exitonclick()
An alternative approach in place of turtle.hideturtle() above is:
turtle.shape("blank")

Related

I'm not sure how to add this image to the screen (turtle)

I entered this turtle code to print a cookie on the screen, but the cookie doesn't actually show up.
import turtle
wn = turtle.Screen()
wn.title("Cookie Clicker")
wn.bgcolor("black")
wn.mainloop()
cookie = "cookie.gif"
turtle.register_shape(cookie)
turtle.shape(cookie)
Anyone know how to fix this? By the way I'm using PyCharm and 'cookie.gif' is in the project folder.
Assuming this isn't an animated GIF, the problem is you've put wn.mainloop() in the wrong place in your code. Generally, it's the last thing you do in a turtle program:
from turtle import Screen, Turtle
cookie = "cookie.gif"
screen = Screen()
screen.title("Cookie Clicker")
screen.bgcolor('black')
screen.register_shape(cookie)
turtle = Turtle()
turtle.shape(cookie)
screen.mainloop()

is there a possibility to make some lines in image in python with turtle?

i am trying to make some lines or draw somthing in GIF image in python with turtle but the line show before the picture .please help this is my code
import turtle
screen = turtle.getscreen()
t1 = turtle.Turtle()
screen.addshape('white.gif')
t1.shape('white.gif')
t1.fd(100)
t1.rt(90)
turtle.done()
If you're trying to draw on an image, you can make the image the background, and then use the turtle to draw onto it:
from turtle import Screen, Turtle
screen = Screen()
turtle = Turtle()
screen.bgpic("white.gif")
turtle.forward(100)
turtle.right(90)
screen.exitonclick()

Python 3 Turtle Graphics: Mouse coordinates [duplicate]

I'm assigned to create a similar version of slither.io in python. I planned on using Turtle. How do I make the turtle follow my mouse without having to click every time?
This is how I would do it when clicking, but I would rather not have to click:
from turtle import *
turtle = Turtle()
screen = Screen()
screen.onscreenclick(turtle.goto)
turtle.getscreen()._root.mainloop()
The key to it is to use the ondrag() event handler on a turtle. A short and not so sweet solution:
import turtle
turtle.ondrag(turtle.goto)
turtle.mainloop()
which will likely crash shortly after you start dragging. A better solution with a larger turtle to drag, and that turns off the drag handler inside the drag hander to prevent events from piling up:
from turtle import Turtle, Screen
def dragging(x, y):
yertle.ondrag(None)
yertle.setheading(yertle.towards(x, y))
yertle.goto(x, y)
yertle.ondrag(dragging)
screen = Screen()
yertle = Turtle('turtle')
yertle.speed('fastest')
yertle.ondrag(dragging)
screen.mainloop()
Note that you have to click and drag the turtle itself, not just click somewhere on the screen. If you want to get the turtle to follow the mouse without keeping the left button held down, see my answer to Move python turtle with mouse pointer.

Python Turtle - change displayed background image

I am writing a Python 3 turtle example program, and am trying to use bgpic() to change the displayed background image.
Here's a simplified version of my program:
import turtle
import time
screen = turtle.Screen()
screen.setup(600,400)
screen.bgpic('image1.gif')
time.sleep(2)
screen.bgpic('image2.gif')
When I run this program, I'd like to see the initial image, and then see the image change after 2 seconds. Instead, the screen stays blank until after the second image is drawn.
Any help appreciated,
Thanks!
Add screen.update() after the first screen.bgpic('image1.gif').
Adding a turtle and moving it made it work for me
import turtle
t = turtle.Turtle()
s = turtle.Screen()
s.setup(300, 300)
s.bgpic('image1.gif')
for i in range(180):
t.color("black")
t.fd(20)

Python turtle.ondrag not working

I've been trying to make a paint program in Python Turtle and for some reason it won't work. I'm using the pen() tool and my code looks like this
from turtle import *
import random
pen()
bgcolor('black')
pencolor('white')
pen.ondrag(pen.goto)
listen()
mainloop()
I've look at this http://docs.python.org/2/library/turtle.html and it says to type turtle.ondrag(turtle.goto) but since I'm using the pen it should work as pen.ondrag but it doesn't, so can someone please clear this up.
Thanks Jellominer
I will simplify and clarify the code given by the questionner:
from turtle import *
ts = Screen(); tu = Turtle()
ts.listen()
ondrag(tu.goto)
mainloop()
This works. You have to click on the turtle and drag it.
First, pen() is not the function you want. Second, although Pen is a synonym for Turtle, pen is not a synonym for turtle. Here's how to go about using ondrag() if you'd like to use Pen instead of Turtle:
from turtle import Pen, Screen, mainloop
def ondrag_handler(x, y):
pen.ondrag(None) # disable handler inside handler
pen.setheading(pen.towards(x, y)) # turn toward cursor
pen.goto(x, y) # move toward cursor
pen.ondrag(ondrag_handler)
screen = Screen()
screen.bgcolor('black')
pen = Pen()
pen.color('white')
pen.shapesize(2) # make it larger so it's easier to drag
pen.ondrag(ondrag_handler)
screen.listen()
mainloop() # screen.mainloop() preferred but not in Python 2
The turtle.ondrag(turtle.goto) makes for a nice short example in the documentation but in reality isn't practical. You want to disable the event handler while handling the event otherwise the events stack up against you. And it's nice to turn the mouse towards your cursor as you drag it.
from turtle import *
ts = Screen()
ondrag(goto)
shapesize(10)
pensize(40)
speed(0)
mainloop()
I think this will surely work.
You can change the size and other things
In here you are using the default turtle .
Sorry but you'll need to take care of the indentation

Categories