py用turtle输出年分秒
时间: 2023-12-07 20:06:09 浏览: 67
Tree4.py 使用turtle画一颗树,树枝桠数量随机生成
可以通过以下代码使用turtle库输出年分秒:
```python
import turtle
import datetime
# 获取当前时间
now = datetime.datetime.now()
# 输出年份
turtle.write("Year: {}".format(now.year), align="center", font=("Arial", 16, "normal"))
turtle.penup()
turtle.forward(100)
turtle.pendown()
# 输出月份
turtle.write("Month: {}".format(now.month), align="center", font=("Arial", 16, "normal"))
turtle.penup()
turtle.forward(100)
turtle.pendown()
# 输出日期
turtle.write("Day: {}".format(now.day), align="center", font=("Arial", 16, "normal"))
turtle.penup()
turtle.backward(200)
turtle.right(90)
turtle.forward(50)
turtle.left(90)
turtle.pendown()
# 输出小时
turtle.write("Hour: {}".format(now.hour), align="center", font=("Arial", 16, "normal"))
turtle.penup()
turtle.forward(100)
turtle.pendown()
# 输出分钟
turtle.write("Minute: {}".format(now.minute), align="center", font=("Arial", 16, "normal"))
turtle.penup()
turtle.forward(100)
turtle.pendown()
# 输出秒钟
turtle.write("Second: {}".format(now.second), align="center", font=("Arial", 16, "normal"))
turtle.done()
```
这段代码会在窗口中输出当前时间的年份、月份、日期、小时、分钟和秒钟。
阅读全文