python七段数码管计时
时间: 2023-11-01 14:08:51 浏览: 94
以下是Python实现七段数码管计时的代码:
```python
import time
# 数码管字典
digit_dict = {
'0': ['1111110', '1010000', '1010000', '1010000', '1010000', '1010000', '1111110'],
'1': ['0100100', '1101100', '0001000', '0001000', '0001000', '0001000', '1111110'],
'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', '0000100', '0001000', '0010000', '0100000'],
'8': ['1111110', '1010000', '1010000', '1111110', '1010000', '1010000', '1111110'],
'9': ['1111110', '1010000', '1010000', '1111110', '0000010', '0000010', '1111110']
}
# 获取当前时间
def get_time():
return time.strftime('%H%M%S', time.localtime(time.time()))
# 显示数码管
def show_digit(digit):
for i in range(7):
for j in range(7):
if digit[i][j] == '1':
print('*', end='')
else:
print(' ', end='')
print()
# 主函数
def main():
while True:
# 获取当前时间
current_time = get_time()
# 分离出时、分、秒
hour = current_time[0:2]
minute = current_time[2:4]
second = current_time[4:6]
# 显示时、分、秒
show_digit(digit_dict[hour[0]])
show_digit(digit_dict[hour[1]])
print(' ', end='')
show_digit(digit_dict[minute[0]])
show_digit(digit_dict[minute[1]])
print(' ', end='')
show_digit(digit_dict[second[0]])
show_digit(digit_dict[second[1]])
# 暂停一秒
time.sleep(1)
# 清屏
print('\033c', end='')
if __name__ == '__main__':
main()
```
阅读全文