📜  在Python中使用 Turtle 绘制时钟设计

📅  最后修改于: 2022-05-13 01:55:12.385000             🧑  作者: Mango

在Python中使用 Turtle 绘制时钟设计

先决条件:用Python进行海龟编程

Turtle 是Python中的一个内置模块。它使用屏幕(纸板)和海龟(笔)提供绘图。要在屏幕上绘制一些东西,我们需要移动海龟(笔)。要移动海龟,有一些函数,即 forward()、backward() 等。

绘制时钟设计:

下面是实现:

Python3
# import package
import turtle
 
# create a Screen Object
screen = turtle.Screen()
 
# Screen configuration
screen.setup(500, 500)
 
# Make turtle Object
clk = turtle.Turtle()
 
# set a Turtle object color
clk.color('Green')
 
# set a Turtle object width
clk.width(4)
 
 
def draw_hour_hand():
    clk.penup()
    clk.home()
    clk.right(90)
    clk.pendown()
    clk.forward(100)
 
 
# value for numbers in clock
val = 0
 
# loop for print clock numbers
for i in range(12):
    # increment value by 1
    val += 1
 
    # move turtle in air
    clk.penup()
 
    # for circular motion
    clk.setheading(-30 * (i + 3) + 75)
 
    # move forward for space
    clk.forward(22)
 
    # move turtle to surface
    clk.pendown()
 
    # move forward for dash line
    clk.forward(15)
 
    # move turtle in air
    clk.penup()
 
    # move forward for space
    clk.forward(20)
 
    # write clock integer
    clk.write(str(val), align="center",
              font=("Arial",
                    12, "normal"))
 
# colored centre by setting position
# sets position of turtle at given position
clk.setpos(2, -112)
clk.pendown()
clk.width(2)
 
# To fill color green
clk.fillcolor('Green')
 
# start filling
clk.begin_fill()
 
# make a circle of radius 5
clk.circle(5)
 
# end filling
clk.end_fill()
 
clk.penup()
draw_hour_hand()
clk.setpos(-20, -64)
clk.pendown()
clk.penup()
 
# Write Clock by setting position
clk.setpos(-30, -170)
clk.pendown()
clk.write(' GfG Clock', font=("Arial", 14,
                              "normal"))
clk.hideturtle()
turtle.done()


输出: