I am following a tutorial for Python learning and I can't get the screen to open to draw. I don't get an error, it just shows that the program finish running.
Maybe I missed something, can someone point it out?
import turtle #acutally called turtle to draw a turtle beautiful also used
to draw other stuff
# to draw a square or eventually a turtle you need to do this things below
# to draw a square you want to : move forward,turn right,move forward,turn
right,move forward turn right
def draw_square(): #draw square for turtles
window = turtle.Screen() #this is the background where the turtle will
move
window.bgcolor("red") # the color of the screen
brad = turtle.Turtle() #this is how to start drawing like time.sleep you use turtle.Turtle
brad.forward(100)#move turtle forward takes in a number which is the distance to move forward
brad.forward(90)# moves right
brad.forward(100)
brad.forward(90)
brad.forward(100)
brad.forward(90)
brad.forward(100)
brad.forward(90)
window.exitonclick() #click the screen to close it
draw_square()
Your primary error is these two lines are in the wrong order:
window.exitonclick() #click the screen to close it
draw_square()
The exitonclick(), or mainloop(), or done() should be the last thing your turtle code does as they turn control over to Tk's event loop. A rework of your code for the above and style issues:
import turtle
# to draw a square, or eventually a turtle, you need to do the things below
def draw_square():
""" draw square for turtles """
# to draw a square you want to : move forward, turn right,
# move forward, turn right,move forward turn right
brad = turtle.Turtle()
brad.forward(100) # forward takes a number which is the distance to move
brad.right(90) # turn right
brad.forward(100)
brad.right(90)
brad.forward(100)
brad.right(90)
brad.forward(100)
brad.right(90)
window = turtle.Screen()
# this is the background where the turtle will move
window.bgcolor("red") # the color of the window
draw_square()
window.exitonclick() # click the screen to close it
To make the Turtle screen stay after the code is executed, you have to either use the turtle.done() or window.exitonclick().
I have created a sample Python program for that, just have a look:
import turtle
me = turtle.Turtle() # creating the turtle object
def drawSquare(size):
for i in range(4):
me.fd(size)
me.rt(90)
me.pencolor('white') # making the pencolor white, so that it is visible in black screen
window = turtle.Screen()
window.bgcolor('black') # creating black background
me.penup()
# repositioning the turtle
me.bk(10)
me.rt(90)
me.bk(50)
# putting the pen down to start working
me.pendown()
# calling the draw square method with the edge length for the square
drawSquare(100)
# window.exitonclick() # exits the screen when clicked
turtle.done() # you have to cross the window to close it
import turtle
t = turtle.Turtle()
t.begin_fill()
for i in range(4):
t.fd(100)
t.lt(90)
t.end_fill()
t.done()
This is the easiest way to draw a square. You begin by importing the turtle, and letting it begin fill. You then go into a for loop which makes the code repeat itself for as many times as you would like (in this case 4). fd is an abbreviation for forward, and the number entered is in pixels. Again, lt is an abbreviation that means left turn, and the number in parentheses is the degrees to be turned. The end_fill command finalizes the fill-in, and t.done keeps the window open until the user closes it.
import turtle
def draw_square():
window = turtle.Screen()
window.bgcolor("green")
bob = turtle.Turtle()
bob.forward(100)
bob.right(90)
bob.forward(100)
bob.right(90)
bob.forward(100)
bob.right(90)
bob.forward(100)
bob.right(90)
window.exitonclick()
draw_square()
Related
I am new with Turtle python library and I am trying to draw turtle dot inside the shape (square), which should look like on the picture below. The problem is that when I am trying to do this the shape covers the dot and I see only the shape (square).
enter image description here
My code:
def add_dot_square():
obj = Turtle()
obj.penup()
obj.shape("square")
obj.shapesize(1.5, 1.5)
obj.color("orange")
obj.goto(0, 0)
obj.dot(20, "red")
Turtle's can't appear behind things they draw, only other turtles (and even that's tricky.) Instead of the turtle being the square, have the turtle draw or stamp the square, and then place the dot atop it:
from turtle import Screen, Turtle
def add_dot_square(obj):
obj.penup()
obj.shape('square')
obj.shapesize(1.5)
obj.color('orange')
obj.goto(0, 0)
obj.stamp()
obj.dot(20, 'red')
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
add_dot_square(turtle)
screen.exitonclick()
I still ha[v]e a problem when I want to for example move this
square and dot
Let's rearrange the code a bit and add some motion:
from turtle import Screen, Turtle
def add_dot_square(obj):
obj.clear()
obj.stamp()
obj.dot(20, 'red')
screen = Screen()
screen.tracer(False)
turtle = Turtle()
turtle.hideturtle()
turtle.shape('square')
turtle.shapesize(1.5)
turtle.color('orange')
turtle.penup()
for _ in range(360):
turtle.circle(100, extent=1)
add_dot_square(turtle)
screen.update()
screen.exitonclick()
How to make the cursor go to a random place on the screen with randint() using Python turtle graphics:
stars = Turtle()
drawings = [Trees, Moon, BlueSky, NightSky, Sun, Sunset, stars]
def penup():
for x in drawings:
x.penup()
penup()
def Stars():
for x in range(5):
penup()
stars.goto((randint(-100,0), randint(0,100)))
pendown()
stars.left(36)
stars.forward(10)
stars.left(36)
penup()
Stars()
I'm trying to make a program that draws stars at the top of the screen, with random spots to where it goes in the night sky. Could someone help with this randint() function? It's not going to a random position and it's acting weird.
Your code is a bit of a mess. Having both stars and Stars is risky coding. You use your penup() function as if it's turtle's penup() function, which it isn't. Your code doesn't even draw one star, let alone many. And the star you're attempting to draw looks more like a pentagon than a star.
Here's a rework of your code that does what you describe:
from turtle import Turtle, Screen
from random import randint
def stars():
for _ in range(20):
star.penup()
star.goto(randint(-200, 200), randint(100, 200))
star.pendown()
for _ in range(5):
star.left(144)
star.forward(10)
screen = Screen()
star = Turtle()
star.speed('fastest') # because I have no patience
stars()
star.hideturtle()
screen.exitonclick()
I am trying to draw a "Yellow" star by using module turtle. When I run my program on Windows OS, it works correctly. However, when I run it on macOS, the graphic is wrong.
Result on macOS
Result on Windows OS
import turtle
# Setup a screen and a turtle
win = turtle.Screen()
bob = turtle.Turtle()
# set the background color for the flag
win.bgcolor("red")
# Draw a star
# change the turtle color to yellow
bob.color("yellow")
# to center we have to go backward for half of a side length
bob.penup()
bob.back(100)
bob.pendown()
bob.begin_fill()
for i in range(5):
bob.forward(200)
bob.right(144)
bob.end_fill()
win.exitonclick()
This is not a turtle problem, but an issue with the underlying tkinter library. The fill on the two operating systems is different when there are crossing lines involved. The solution is to draw the star without crossing lines:
from turtle import Screen, Turtle
win = Screen()
win.bgcolor("red")
bob = Turtle()
bob.color("yellow")
bob.penup()
bob.goto(24.5, 33.1)
bob.pendown()
bob.begin_fill()
for i in range(5):
bob.forward(80)
bob.right(144)
bob.forward(80)
bob.left(72)
bob.end_fill()
bob.hideturtle()
win.exitonclick()
This should look the same on both implementations:
Turtle docs say when end_poly() is reached:
Stop recording the vertices of a polygon. Current turtle position is
last vertex of polygon. This will be connected with the first vertex.
With my example, the final line is not drawn from the last vertex back to the first vertex. It acts the same in 2.7 and 3.7 Python.
from turtle import *
print("position 0:",position())
width(5)
pencolor("red")
fillcolor("blue")
begin_fill()
begin_poly()
fd(100)
print("position 1:",position())
left(90)
fd(100)
print("position 2:",position())
end_poly()
end_fill()
p = get_poly()
print("poly p:",p)
register_shape("myShape",p)
shapes_ = getshapes()
print("shapes_:", shapes_)
Output:
position 0: (0.00,0.00)
position 1: (100.00,0.00)
position 2: (100.00,100.00)
poly p: ((0.00,0.00), (100.00,0.00), (100.00,100.00))
shapes_: ['arrow', 'blank', 'circle', 'classic', 'myShape', 'square', 'triangle', 'turtle']
Image of polygon
I believe this is an error in the documentation, but an understandable one.
First, you clearly can draw what you want by closing the figure yourself:
from turtle import Turtle, Screen
screen = Screen()
turtle = Turtle(visible=False)
turtle.width(5)
turtle.color("red", "blue")
position = turtle.position()
turtle.begin_fill()
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.goto(position)
turtle.end_fill()
screen.exitonclick()
So what's the deal with polygons? Well, unless you implement the code yourself, there's nothing turtle can do, by default, with polygons, except use them as turtle cursors. In which case it's passing them onto tkinter which is responsible for the closing the polygon:
from turtle import Turtle, Screen
screen = Screen()
turtle = Turtle(visible=False)
turtle.penup()
turtle.begin_poly()
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.end_poly()
screen.register_shape("myShape", turtle.get_poly())
turtle.shape("myShape") # when the polygon gets closed
turtle.shapesize(outline=5)
turtle.color("red", "blue")
turtle.stamp()
screen.exitonclick()
(I'm guessing it's in tkinter's canvas.coords() where the polygon gets closed.) Note that your width(5) means nothing in this case, nor does *_fill() as a cursor's outline width is set using shapesize() and a cursor is naturally filled. It doesn't need a color specified at polygon creation time, you can wait until the new cursor is deployed.
I believe this statement in the end_poly() documentation:
last vertex of polygon. This will be connected with the first vertex.
Should really reside in the polygon section of turtle's register_shape() documentation. But you can understand the error as turtle only thinks the role of *_poly() is for creating new cursors. Whereas polygons should be a first class, flexible datatype in turtle.
So far I have this and it makes two circles but one is off-screen. I want to center it and have them separate from each other. Right now it does two loops but I want it to do one small circle then go on to make a larger one around the first in the middle of the screen. Both need to be diff. colors.
def sun_and_earth():
import turtle #allows me to use the turtles library
turtle.Turtle()
turtle.Screen() #creates turtle screen
turtle.window_height()
turtle.window_width()
turtle.bgcolor('grey') #makes background color
turtle.color("red", "green")
turtle.circle(2, 360) #draws a (size, radius) circle
turtle.circle(218, 360)
turtle.exitonclick() #exits out of turtle window on click of window
I think you may have some misunderstanding with regard to some of the functions in the turtle library. Firstly, turtle.window_height() and turtle.window_width() return the height and width of the window, so (as these values are not being assigned) those two lines do nothing. Similarly, turtle.Screen() returns an object, so again that line does nothing.
In order to centre your circles, you need to change where the turtle starts by using the turtle.setpos() function. This will change the x and y coordinates of where your turtle is. If you start the turtle one radius down, this will effectively centre the circle at (0, 0), because the center of the circle is (from the documentation) one radius to the left.
Remember to take your pen off the page when you are moving so that you don't draw lines between the two points by accident, and to put the pen back down again when you want to draw again.
Try this code:
import turtle
turtle.Turtle()
turtle.bgcolor('grey')
# decide what your small circle will look like
smallColour = "red"
smallRadius = 5
# draw the small circle
turtle.color(smallColour)
turtle.penup()
turtle.setpos(0, -smallRadius)
turtle.pendown()
turtle.circle(smallRadius)
# decide what your large circle will look like
largeColour = "white"
largeRadius = 100
# draw the large circle
turtle.color(largeColour)
turtle.penup()
print(turtle.pos())
turtle.setpos(0, -largeRadius)
print(turtle.pos())
turtle.pendown()
turtle.circle(largeRadius)
turtle.penup()
turtle.setpos(0, 0)
I hope this helps, but I think that you have a few misunderstandings about the use of the turtle, it might be a good idea to look at a tutorial or maybe take a look at the documentation
Best of luck