欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python控制台显示时钟的示例

程序员文章站 2023-10-20 16:14:55
复制代码 代码如下:#!/usr/bin/env python# coding: utf-8### show time in console#import sysimpor...

复制代码 代码如下:

#!/usr/bin/env python
# coding: utf-8
#
#
# show time in console
#
import sys
import time

raws = '''
.--.

|  |

`--`
  .
 /|

  |
 ---
---.

---`

`---
---.

---|

---`
.  .

`--|

   |
.---

`--.

---`
.---

|--.

`--`
.--.

`  |

   |
.--.

|--|

`--`
.--.

`--|

---`
'''.strip()
numbers = {}
def init():
    for num in range(10):
        numbers[str(num)] = []
    lineno = 0
    for line in raws.split('\n'):
        line = line.ljust(4)
        arr = []
        for char in line:
            arr.append(char) # != ' ')
        numbers[str(lineno/5)].append(arr)
        lineno += 1
    numbers[':'] = [[' ', ' ', ' ', ' '], [' ', ' ', '-', ' '], [' ', ' ', ' ', ' '], [' ', ' ', '-', ' '], [' ', ' ', ' ', ' ']]
    numbers[' '] = [[' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ']]
def print_num(digtal):
    digtal = str(digtal)
    screen = []
    for i in range(5):
        screen.append([])
    for num in digtal:
        for i, linechar in enumerate(numbers[num]):
            for char in linechar:
                screen[i].append(char)
            screen[i].append('   ')
    for line in screen:
        print ''.join(line)
init()
def cls():
    sys.stdout.write('\033[2j\033[0;0h')
    sys.stdout.flush()

while true:
    t = time.strftime("%h:%m:%s")
    cls(); print_num(t)
    time.sleep(1)
    t = time.strftime("%h %m %s")
    cls(); print_num(t)
    time.sleep(1)