python简单程序以如图格式绘制7段数码管,输出当前时间
时间: 2023-08-05 18:22:14 浏览: 71
细讲用Python绘制数码管显示当前时间
好的,以下是Python程序以7段数码管格式绘制当前时间的代码:
```python
import time
# 7段数码管的编码
digits = {
0: ['1111110', '1010000', '1010000', '1010000', '1010000', '1010000', '1111110'],
1: ['0100100', '0010100', '0010100', '0010100', '0010100', '0010100', '0111110'],
2: ['1111110', '0000010', '0000010', '1111110', '1000000', '1000000', '1111110'],
3: ['1111110', '0000010', '0000010', '1111110', '0000010', '0000010', '1111110'],
4: ['1010000', '1010000', '1010000', '1111110', '0000010', '0000010', '0000010'],
5: ['1111110', '1000000', '1000000', '1111110', '0000010', '0000010', '1111110'],
6: ['1111110', '1000000', '1000000', '1111110', '1010000', '1010000', '1111110'],
7: ['1111110', '0000010', '0000010', '0000010', '0000010', '0000010', '0000010'],
8: ['1111110', '1010000', '1010000', '1111110', '1010000', '1010000', '1111110'],
9: ['1111110', '1010000', '1010000', '1111110', '0000010', '0000010', '1111110']
}
def print_digit(digit):
for i in range(7):
for j in range(7):
if digits[digit][i][j] == '1':
print('*', end='')
else:
print(' ', end='')
print()
while True:
# 获取当前时间
current_time = time.strftime("%H:%M:%S", time.localtime())
# 清屏
print('\033c')
# 输出当前时间
for i in range(7):
for j in range(len(current_time)):
if current_time[j].isdigit():
print_digit(int(current_time[j])[i], end='')
else:
if i == 2 or i == 4:
print(':', end='')
else:
print(' ', end='')
print()
# 暂停1秒
time.sleep(1)
```
这个程序会在终端窗口中以7段数码管的形式输出当前时间,并且每隔1秒钟会自动更新一次。
阅读全文