Python: terminal 內的文字動畫 - 图1
© getcodify.com

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

Animation-Text

  1. class Signal: # <1>
  2. go = True
  3. def spin(msg, signal): # <2>
  4. write, flush = sys.stdout.write, sys.stdout.flush
  5. for char in itertools.cycle('|/-\\'): # <3>
  6. status = char + ' ' + msg
  7. write(status)
  8. flush()
  9. time.sleep(.1)
  10. write('\x08' * len(status)) # <4>
  11. if not signal.go:
  12. break
  13. write(' ' * len(status) + '\x08' * len(status)) # <6>
  14. def slow_function(): # <7>
  15. # pretend waiting a long time for I/O
  16. time.sleep(3) # <8>
  17. return 42
  18. def supervisor(): # <9>
  19. signal = Signal()
  20. spinner = threading.Thread(target=spin,
  21. args=('thinking!', signal))
  22. print('spinner object:', spinner) # <10>
  23. spinner.start() # <11>
  24. result = slow_function() # <12>
  25. signal.go = False # <13>
  26. spinner.join() # <14>
  27. return result
  28. def main():
  29. result = supervisor() # <15>
  30. print('Answer:', result)
  31. if __name__ == '__main__':
  32. main()

Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗