使用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)
绘制效果: