首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python Turtle中绘制一个正方形

在Python Turtle中绘制一个正方形
EN

Stack Overflow用户
提问于 2017-09-07 01:52:46
回答 2查看 17.1K关注 0票数 2

我正在学习Python的教程,但我无法打开屏幕来绘图。我没有得到一个错误,它只是表明程序已经结束运行。也许我漏掉了什么,有人能指出一下吗?

代码语言:javascript
复制
 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()
EN

回答 2

Stack Overflow用户

发布于 2020-05-10 13:50:40

要使海龟屏幕在代码执行后保持不动,必须使用turtle.done()window.exitonclick()

我已经为此创建了一个样例Python程序,只需看一下:

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2018-06-18 05:09:29

代码语言:javascript
复制
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()
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46081500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档