J1N1
J1N1

Reputation: 39

Draw a square in Python Turtle

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

Upvotes: 2

Views: 23667

Answers (4)

HSaunders603
HSaunders603

Reputation: 41

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.

Upvotes: 1

Tuhin Mitra
Tuhin Mitra

Reputation: 727

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

Upvotes: 0

Alex Cazi
Alex Cazi

Reputation: 1

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

Upvotes: -1

cdlane
cdlane

Reputation: 41872

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

Upvotes: 3

Related Questions