Why does the screen with turtle graphic close down by itself - python

Why does the screen with turtle graphic close down by itself?
import turtle
circle = turtle.Turtle()
circle.color("pink")
circle.pensize(5)
circle.speed(5)
circle.circle(10)
circle.done()

Instead of circle.done(), put turtle.done()

The turtle window closed by itself because your program ran into an error:
AttributeError: 'Turtle' object has no attribute 'done'
Use turtle.done() instead:
import turtle
circle = turtle.Turtle()
circle.color("pink")
circle.pensize(5)
circle.speed(5)
circle.circle(10)
turtle.done()
See the documentation here.

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()

turtle showing error which used to work fine earlier

import turtle
t = turtle.Turtle()
t.forward(100)
t.done()
this code is returning an error like this
NameError: name 'turtle' is not defined
I have tried to use turtle but it doesn't seem to be working the way it is shown in the code.
I have tried referring to various sources but could not find the fix
The problem lies on this line:
t.done()
You see, you defined t as a Turtle object, and Turtle objects have no attribute done.
The done() attribute belongs to the turtle module itself, so instead of t.done(), it's turtle.done():
import turtle
t = turtle.Turtle()
t.forward(100)
turtle.done()

Getting error with turtle.bgcolor() in Python

I am trying to use turtle to create a square, while tryng to give a color to the turtle window a color "black". I am getting errors, any input will be helpful.
Below is my code:
import turtle
def draw_square():
window = turtle._Screen
window.bgcolor("black")
brad = turtle.Turtle()
brad.forward(100)
window.exitonclick()
draw_square()
Below is what I get in error:
color = self._bgcolor(color)
AttributeError: 'str' object has no attribute '_bgcolor'
turtle._Screen gives you the Screen class, not a Screen object. Use turtle.getscreen() instead.
General issues with your code:
Don't access methods, or variables, whose names begin with underscore "_". These are internal methods and not for you, if you're new to this library. Find high level methods in the documentation.
If you set the background color to black, you need to change the pen color, otherwise you're drawing a black square on a black background and it won't be visible.
Don't call window methods in a function called draw_square() -- it shouldn't know the state of the screen in order to draw a square. Handle these details outside of the function.
Here's a rework of your code along following the above guidelines:
import turtle
def draw_square():
brad = turtle.Turtle()
brad.pencolor("white")
for _ in range(4):
brad.forward(100)
brad.right(90)
window = turtle.Screen()
window.bgcolor("black")
draw_square()
window.exitonclick()

mainloop function from turtle class is not working, Python

This is my code :
import turtle
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(50)
alex.left(90)
alex.forward(30)
wn.mainloop()
And my error is: "_Screen object has no attribute mainloop"
I tried to do just "mainloop" but then I got NameError while doing alex.mainloop gave same error i.e. turtle object has no attribute mainloop.
Googling told me that I might have my file name as turtle.py creating the conflict but that's not the case.
You just need to call the mainloop() function on the turtle module. So your last line should be:
turtle.mainloop()

Categories