简体   繁体   中英

Python: Turtle can't call function twice

I have a question, I was trying to draw a square and a circle in Python (2.7) using the turtle module.

import turtle
def draw_cricle(circle_size):

    boby = turtle.Turtle()

    boby.color("black")
    boby.shape("arrow")

    boby.right(90)
    boby.forward(200)
    boby.left(90)
    boby.back(20)

    boby.circle(circle_size)

def draw_square(forward_dst, right_angle):
    window = turtle.Screen()

    window.bgcolor("red")
    brad = turtle.Turtle()
    brad.shape("circle")
    brad.speed(3)

    brad.forward(forward_dst)
    brad.right(right_angle)
    brad.forward(forward_dst)
    brad.right(right_angle)
    brad.forward(forward_dst)
    brad.right(right_angle)
    brad.forward(forward_dst)
    brad.right(right_angle)
    window.exitonclick()

draw_square(100,90)

draw_cricle(100)

My problem is that I can't call the the draw_circle function after calling the draw_square function.

And yes I know I should have used a loop in draw_square

You are closing the window when running window.exitonclick() from the draw_square function. You should define the window outside of these functions if you want to use the same window to draw both square and circle, otherwise, you're closing the window before even starting to draw a circle.

import turtle


def draw_cricle(circle_size):

    boby = turtle.Turtle()

    boby.color("black")
    boby.shape("arrow")

    boby.right(90)
    boby.forward(200)
    boby.left(90)
    boby.back(20)

    boby.circle(circle_size)

def draw_square(forward_dst, right_angle):

    brad = turtle.Turtle()
    brad.shape("circle")
    brad.speed(3)

    brad.forward(forward_dst)
    brad.right(right_angle)
    brad.forward(forward_dst)
    brad.right(right_angle)
    brad.forward(forward_dst)
    brad.right(right_angle)
    brad.forward(forward_dst)
    brad.right(right_angle)

window = turtle.Screen()
window.bgcolor("red")

draw_square(100,90)
draw_cricle(100)


window.exitonclick()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM