-
-
Save gbnk0/88431df04bd8bd163a3a8b2e86f2b866 to your computer and use it in GitHub Desktop.
Python Text Cursor Animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# taken from: http://stackoverflow.com/questions/7039114/waiting-animation-in-command-prompt-python | |
import threading | |
import time | |
class CursorAnimation(threading.Thread): | |
def __init__(self): | |
self.flag = True | |
self.animation_char = "|/-\\" | |
self.idx = 0 | |
threading.Thread.__init__(self) | |
def run(self): | |
while self.flag: | |
print "Processing... ", | |
print self.animation_char[self.idx % len(self.animation_char)] + "\r", | |
self.idx += 1 | |
time.sleep(0.1) | |
def stop(self): | |
self.flag = False | |
if __name__ == '__main__': | |
spin = CursorAnimation() | |
# Start Animation | |
spin.start() | |
# Do something here | |
# Example: sleep | |
time.sleep(5) | |
# Stop Animation | |
spin.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment