# Author:Marvin Mao
# 时钟.py
import time
# 时钟程序
class Clock(object):
def __init__(self, h=0, m=0, s=0):
"""
设置初始化时分秒
:param h: 时
:param m: 分
:param s: 秒
"""
self.h = h
self.m = m
self.s = s
def clock_run(self):
"""
定义计时规则:
当秒钟计数到60时,秒钟清零分钟计数+1
当分钟计数到60时,分钟清零时钟计数+1
当时钟计数到24时,时钟清零,一天轮回
"""
self.s = self.s + 1
if self.s == 60:
self.s = 0
self.m = self.m + 1
if self.m == 60:
self.m = 0
self.h = self.h + 1
if self.h == 24:
self.h = 0
def show_clock(self):
"""
按照HH:MM:SS格式打印时钟,并持续计时
"""
print("\r%02d:%02d:%02d" % (self.h, self.m, self.s), end='')
def main():
current_time = time.localtime(time.time())
c = Clock(current_time.tm_hour, current_time.tm_min, current_time.tm_sec)
while 1:
c.show_clock()
time.sleep(1)
c.clock_run()
if __name__ == '__main__':
main()
Python写的一个小的计时时钟程序
最新推荐文章于 2024-01-13 18:33:46 发布