下面是一个简单的Python程序,可以绘制数码管来显示当前时间:
```python
import time
import turtle
# 数字字典,每个数字对应的7段数码管的状态
digits = {
'0': [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0)],
'1': [(1, 0), (1, 1)],
'2': [(0, 0), (1, 0), (1, 1), (2, 1), (3, 0)],
'3': [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)],
'4': [(0, 1), (1, 0), (1, 1), (2, 1)],
'5': [(0, 0), (0, 1), (1, 1), (2, 0), (2, 1)],
'6': [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)],
'7': [(0, 0), (1, 0), (1, 1)],
'8': [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0)],
'9': [(0, 0), (0, 1), (1, 0), (1, 1), (2, 1)]
}
# 绘制一个7段数码管
def draw_digit(x, y, digit):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for seg in digit:
turtle.setheading(seg[0] * 90)
turtle.forward(seg[1] * 10)
# 获取当前时间,格式为HH:MM:SS
def get_time():
return time.strftime('%H:%M:%S')
# 绘制当前时间的数码管
def draw_time():
turtle.clear()
turtle.speed(0)
turtle.hideturtle()
turtle.pencolor('red')
turtle.pensize(5)
time_str = get_time()
for i in range(len(time_str)):
if time_str[i] == ':':
continue
digit = digits[time_str[i]]
draw_digit(-150 + i * 80, 0, digit)
# 每隔一秒钟更新一次时间
while True:
draw_time()
time.sleep(1)
```
这个程序使用Python的turtle库来绘制数码管。首先,定义了一个字典digits,其中每个数字对应一个7段数码管的状态。然后,定义了一个函数draw_digit,用于绘制一个7段数码管。接着,定义了一个函数get_time,用于获取当前时间。最后,定义了一个循环,每隔一秒钟更新一次时间,调用draw_time函数来绘制当前时间的数码管。