python程序运用turtle画图以如图格式绘制7段数码管,输出当前年月日
时间: 2024-02-22 20:58:15 浏览: 81
python 用turtle模块绘图 示例
以下是使用turtle绘制7段数码管并输出当前年月日的Python代码:
```python
import turtle
import time
# 定义数码管的线段起始点和终止点坐标
line_dict = {
0: [(0, 60), (30, 90), (30, 210), (0, 240), (-30, 210), (-30, 90)],
1: [(0, 60), (0, 240)],
2: [(0, 60), (30, 90), (30, 150), (-30, 210), (-30, 270)],
3: [(0, 60), (30, 90), (30, 150), (0, 180), (-30, 150), (-30, 90)],
4: [(30, 90), (30, 150), (0, 180), (-30, 150), (0, 120)],
5: [(30, 90), (30, 210), (0, 240), (-30, 210), (-30, 150)],
6: [(30, 90), (30, 210), (0, 240), (-30, 210), (-30, 150), (0, 120)]
}
# 绘制一个数码管
def draw_digit(digit, size):
turtle.pendown()
turtle.setheading(90)
for start, end in line_dict[digit]:
turtle.penup()
turtle.goto(start[0] * size, start[1] * size)
turtle.pendown()
turtle.goto(end[0] * size, end[1] * size)
turtle.penup()
# 绘制当前日期
def draw_date(size):
turtle.clear()
turtle.penup()
turtle.goto(-200, 0)
turtle.pensize(5 * size)
year, month, day = time.strftime('%Y-%m-%d', time.localtime()).split('-')
for digit in year + month + day:
draw_digit(int(digit), size)
turtle.forward(60 * size)
turtle.hideturtle()
# 设置画布大小
turtle.setup(800, 600)
turtle.speed(0)
turtle.hideturtle()
turtle.bgcolor('black')
# 循环绘制日期
while True:
draw_date(5)
time.sleep(1)
```
运行代码后,程序将在窗口中实时显示当前的年月日,每秒钟更新一次。
阅读全文