clock.py 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from time import time, localtime, sleep
  2. class Clock(object):
  3. """数字时钟"""
  4. def __init__(self, hour=0, minute=0, second=0):
  5. self._hour = hour
  6. self._minute = minute
  7. self._second = second
  8. @classmethod
  9. def now(cls):
  10. ctime = localtime(time())
  11. return cls(ctime.tm_hour, ctime.tm_min, ctime.tm_sec)
  12. def run(self):
  13. """走字"""
  14. self._second += 1
  15. if self._second == 60:
  16. self._second = 0
  17. self._minute += 1
  18. if self._minute == 60:
  19. self._minute = 0
  20. self._hour += 1
  21. if self._hour == 24:
  22. self._hour = 0
  23. def show(self):
  24. """显示时间"""
  25. return '%02d:%02d:%02d' % \
  26. (self._hour, self._minute, self._second)
  27. def main():
  28. clock = Clock.now()
  29. while True:
  30. print(clock.show())
  31. sleep(1)
  32. clock.run()
  33. if __name__ == '__main__':
  34. main()