在 Python 海龟中画一个正方形
Draw a square in Python Turtle
我正在学习 Python 教程,但无法打开屏幕进行绘图。我没有收到错误,它只是显示程序完成 运行。
也许我漏掉了什么,有人可以指出吗?
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()
您的主要错误是这两行的顺序错误:
window.exitonclick() #click the screen to close it
draw_square()
exitonclick()
、mainloop()
或 done()
应该是 last 你的 turtle 代码在将控制权移交给Tk 的事件循环。针对上述问题和样式问题修改代码:
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
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()
要使 Turtle 屏幕在代码执行后保持不变,您必须使用 turtle.done()
或 window.exitonclick()
。
我为此创建了一个示例 Python 程序,请看一下:
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()
这是最简单的正方形画法
我正在学习 Python 教程,但无法打开屏幕进行绘图。我没有收到错误,它只是显示程序完成 运行。 也许我漏掉了什么,有人可以指出吗?
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()
您的主要错误是这两行的顺序错误:
window.exitonclick() #click the screen to close it
draw_square()
exitonclick()
、mainloop()
或 done()
应该是 last 你的 turtle 代码在将控制权移交给Tk 的事件循环。针对上述问题和样式问题修改代码:
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
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()
要使 Turtle 屏幕在代码执行后保持不变,您必须使用 turtle.done()
或 window.exitonclick()
。
我为此创建了一个示例 Python 程序,请看一下:
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()
这是最简单的正方形画法