从昨天到现在,一直在研究这个看起来超级简单的时钟。界面非常简洁大方。
虽然是简单,可是这个对齐的问题还是把我整得一塌糊涂。谁叫作者不解释清楚的。
参考:http://bbs.fishc.com/thread-77638-1-1.html
接下来,我们一步步解决。
----------------------------------------------------------------------------------------------------------------
步骤1
注意。这里是换行打印才能打印出这种图形的,这个字典储存的是列表,为啥储存的是列表呢?因为待会
一个数字,我们是要分上中下三个部分打印的。
这里的元素要尽可能对齐,因为这个程序最麻烦的就是要整齐的输出。第一个/位置0元素的笔画统一居中。
以0为例,位置0(一横)除了1和4之外,在其它数字都是给包围起来的,所以两边的空格其实是留给竖的!!!
lcd = {
0: [" _ ", "| |", "|_|"],
1: [" ", " |", " |"],
2: [" _ ", " _|", "|_ "],
3: [" _ ", " _|", " _|"],
4: [" ", "|_|", " |"],
5: [" _ ", "|_ ", " _|"],
6: [" _ ", "|_ ", "|_|"],
7: [" _ ", " |", " |"],
8: [" _ ", "|_|", "|_|"],
9: [" _ ", "|_|", " _|"]
} for num in lcd:
for i in lcd[num]:
print i
如果输出的是这种整整齐齐的数字的话(代码是纵向输出的),说明我们千辛万苦输入的列表元素是正确的。
步骤2
接上面的代码,完整版在下面。
# coding: utf-8 import time # 分别把时,分,秒,拆分成上中下三个部分并
# 分别存储到一个空列表中(empty) def num2lcd(time):
et = ["", "", ""]
shiwei = time / 10
gewei = time % 10 for i in range(0, 3):
et[i] = lcd[shiwei][i] + lcd[gewei][i] return et # time.localtime()是输出当前日期的元组
# t.tm_hour是时,t.tm_min是分钟
# 这个函数最关键的部分是把之前分开的时,
# 分,秒的列表元素集合到一个列表中,最后统一打印出来。 def clock():
t = time.localtime()
h = num2lcd(t.tm_hour)
m = num2lcd(t.tm_min)
s = num2lcd(t.tm_sec) output = [h[0] + m[0] + s[0], h[1] + m[1] + s[1], h[2] + m[2] + s[2]]
return output
注意,这里的时间显示能否对齐完全取决于lcd字典是否对齐,每个值前面和后面的空格数量一定要控制好
---------------------------------------------------------------------------------------------------------------------------
# coding: utf-8 2016/12/8 import os, time # 这里在上面的基础上,每个列表的元素后面加
# 多一个空格,让数字之间显示更美观 lcd = {
0: [" _ ", "| | ", "|_| "],
1: [" ", " | ", " | "],
2: [" _ ", " _| ", "|_ "],
3: [" _ ", " _| ", " _| "],
4: [" ", "|_| ", " | "],
5: [" _ ", "|_ ", " _| "],
6: [" _ ", "|_ ", "|_| "],
7: [" _ ", " | ", " | "],
8: [" _ ", "|_| ", "|_| "],
9: [" _ ", "|_| ", " _| "],
} # 分别把时,分,秒,拆分成上中下三个部分并
# 分别存储到一个空列表中(empty) def num2lcd(time):
et = ["", "", ""]
shiwei = time / 10
gewei = time % 10 for i in range(0, 3):
et[i] = lcd[shiwei][i] + lcd[gewei][i] return et # time.localtime()是输出当前日期的元组
# t.tm_hour是时,t.tm_min是分钟
# 这个函数最关键的部分是把之前分开的时,
# 分,秒的列表元素集合到一个列表中,最后统一打印出来。 def clock():
t = time.localtime()
h = num2lcd(t.tm_hour)
m = num2lcd(t.tm_min)
s = num2lcd(t.tm_sec) output = [h[0] + " " + m[0] + " " + s[0], h[1] + "." + m[1] + "." + s[1], h[2] + "." + m[2] + "." + s[2]]
return output while True:
for i in clock():
print i
time.sleep(1)
os.system("cls")
时钟完整版
由于在时和分,分和秒之间加了冒号,而每个时h[0]和分m[0],分m[0]和秒s[0]之间距离是一定的,
由于冒号自身占用1个空格,所以后面的会变成这样。因此要在h[0]的m[0],m[0]和s[0]之间加上空格。
output = [h[0] + " " + m[0] + " " + s[0], h[1] + "." + m[1] + "." + s[1], h[2] + "." + m[2] + "." + s[2]]
----------------------------------------------------------------------------------------------------------------------------
秒表的同理类似。这里就不再介绍了,直接附上代码。
# coding: utf-8 import os, sys, time lcd = {
0: [" _ ", "| | ", "|_| "],
1: [" ", " | ", " | "],
2: [" _ ", " _| ", "|_ "],
3: [" _ ", " _| ", " _| "],
4: [" ", "|_| ", " | "],
5: [" _ ", "|_ ", " _| "],
6: [" _ ", "|_ ", "|_| "],
7: [" _ ", " | ", " | "],
8: [" _ ", "|_| ", "|_| "],
9: [" _ ", "|_| ", " _| "]
} def num2lcd(time):
et = ["", "", ""]
shiwei = time / 10
gewei = time % 10 for i in range(0, 3):
et[i] = lcd[shiwei][i] + lcd[gewei][i] return et def clock(minute, second):
m = num2lcd(minute)
s = num2lcd(second) output = [m[0] + " " + s[0], m[1] + "." + s[1], m[2] + "." + s[2]] return output sec = 0
while True:
sec += 1
m = sec / 60
s = sec % 60 for i in clock(m, s):
print i time.sleep(1)
os.system("cls")
一般来说,当需要输出时间时,用求余符号%来求出分和秒是比较简便的。或者大家也可
以看看下面这一种。是用for循环在1和60之间循环递增的。选择自己喜欢的一种即可。
# coding: utf-8 import os, sys, time lcd = {
0: [" _ ", "| | ", "|_| "],
1: [" ", " | ", " | "],
2: [" _ ", " _| ", "|_ "],
3: [" _ ", " _| ", " _| "],
4: [" ", "|_| ", " | "],
5: [" _ ", "|_ ", " _| "],
6: [" _ ", "|_ ", "|_| "],
7: [" _ ", " | ", " | "],
8: [" _ ", "|_| ", "|_| "],
9: [" _ ", "|_| ", " _| "]
} def num2lcd(time):
et = ["", "", ""]
shiwei = time / 10
gewei = time % 10 for i in range(0, 3):
et[i] = lcd[shiwei][i] + lcd[gewei][i] return et def clock(minute, second):
m = num2lcd(minute)
s = num2lcd(second) output = [m[0] + " " + s[0], m[1] + "." + s[1], m[2] + "." + s[2]] return output minute = 0
while True:
for sec in range(1, 61):
if sec == 60:
sec = 0 # 记得到了第60秒时秒要清零哦
minute += 1 # 同时分钟要 +1 for i in clock(minute, sec):
print i time.sleep(1)
os.system("cls")
lcd秒表