簡體   English   中英

在 Python Turtle 中畫一個正方形

[英]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()應該是您的海龜代碼在將控制權交給 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

要在代碼執行后讓 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()

這是繪制正方形的最簡單方法。 您首先導入海龜,然后讓它開始填充。 然后,您進入一個 for 循環,該循環使代碼根據您的需要重復多次(在本例中為 4)。 fd 是 forward 的縮寫,輸入的數字以像素為單位。 同樣,lt是左轉的縮寫,括號中的數字是要轉動的度數。 end_fill 命令完成填充,並且 t.done 保持窗口打開,直到用戶關閉它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM