turtle库使用——绘制时钟,同时在时钟上输出文字。

使用write()输出文字
write(arg, move=False, align="left", font=())
arg是要写入海龟窗口的文字对象,move默认是False,如果True画笔将移动到本文右下角,
align是"left""center"或"right",是对齐方式。
如果想自定义字体,可以在font=()内设定(fontname,fontsize,fonttype).
# 使用write()输出文字
# write(arg, move=False, align="left", font=())
# arg是要写入海龟窗口的文字对象,move默认是False,如果True画笔将移动到本文右下角,
# lign是"left""center"或"right"。
# 如果想自定义字体,可以在font=()内设定(fontname,fontsize,fonttype)
import time
import turtle

# 绘制时钟,同时在时钟上方输出文字
t = turtle.Pen()
t.shape('turtle')
t.speed(10)  # 加速绘制图形
# 绘制时钟中间颜色
t.color('yellow', 'aqua')
t.setpos(0, -120)  # 更改海龟坐标
t.begin_fill()
t.circle(120)  # 绘制时钟内圆盘
t.end_fill()
t.penup()
t.home()
t.pendown()
t.color('black')
t.pensize(5)
# 绘制时钟刻度
for i in range(1, 13):
    t.penup()
    t.seth(-30 * i + 90)  # 设定刻度的角度
    t.forward(160)
    t.pendown()
    t.forward(30)  # 画时间轴
    t.penup()
    t.forward(25)
    t.write(str(i), align="left", font=10)  # 写上刻度
    t.home()
# 绘制时钟外框
t.home()
t.setpos(0, -260)
t.pendown()
t.pensize(10)
t.circle(260)


# 写上名字
t.pensize(5)
t.penup()
t.setpos(0, 280)
t.pendown()
t.color('red')
t.write('Python Clock', align="center", font=('新细明体', 24))
t.ht()  # 隐藏光标
time.sleep(5)

绘制效果:

 为了使时钟更加逼真,给上面时钟加上时针、分针、秒针。

# 使用write()输出文字
# write(arg, move=False, align="left", font=())
# arg是要写入海龟窗口的文字对象,move默认是False,如果True画笔将移动到本文右下角,
# lign是"left""center"或"right"。
# 如果想自定义字体,可以在font=()内设定(fontname,fontsize,fonttype)
import time
import turtle

# 绘制时钟,同时在时钟上方输出文字
t = turtle.Pen()
t.shape('turtle')
t.speed(10)  # 加速绘制图形
# 绘制时钟中间颜色
t.color('yellow', 'aqua')
t.setpos(0, -120)  # 更改海龟坐标
t.begin_fill()
t.circle(120)  # 绘制时钟内圆盘
t.end_fill()
t.penup()
t.home()
t.pendown()
t.color('black')
t.pensize(5)
# 绘制时钟刻度
for i in range(1, 13):
    t.penup()
    t.seth(-30 * i + 90)  # 设定刻度的角度
    t.forward(160)
    t.pendown()
    t.forward(30)  # 画时间轴
    t.penup()
    t.forward(25)
    t.write(str(i), align="left", font=10)  # 写上刻度
    t.home()
# 绘制时钟外框
t.home()
t.setpos(0, -260)
t.pendown()
t.pensize(10)
t.circle(260)

# 添加时针、分针、秒针
t.penup()
t.setpos(0, -10)
t.pendown()
t.fillcolor('black')
t.begin_fill()
t.circle(10)
t.end_fill()
# 时针
t.home()
t.pensize(12)
t.pendown()
t.forward(70)
# 分针
t.home()
t.pensize(8)
t.seth(87)
t.forward(100)
# 秒针
t.home()
t.pensize(5)
t.seth(60)
t.forward(130)

# 写上名字
t.pensize(5)
t.penup()
t.setpos(0, 280)
t.pendown()
t.color('red')
t.write('Python Clock', align="center", font=('新细明体', 24))
t.ht()  # 隐藏光标
time.sleep(5)

绘制效果:

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笨小古

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值