我需要根据Python的答案打印出旋转风扇。
import threading import subprocess I = 0 class RepeatingTimer(threading._Timer): def run(self): while True: self.finished.wait(self.interval) if self.finished.is_set(): return else: self.function(*self.args, **self.kwargs) def status(): global I icons = ['|','/','--','\\'] print icons[I] I += 1 if I == 4: I = 0 timer = RepeatingTimer(1.0, status) timer.daemon = True # Allows program to exit if only the thread is alive timer.start() proc = subprocess.Popen([ 'python', "wait.py" ]) proc.wait() timer.cancel()这段代码可以显示风扇,但是回车显示如下。
| / -- \ | / -- ...什么是在不移动插入位置的情况下打印字符的python代码?
I need to print out rotating fan based on this answer with Python.
import threading import subprocess I = 0 class RepeatingTimer(threading._Timer): def run(self): while True: self.finished.wait(self.interval) if self.finished.is_set(): return else: self.function(*self.args, **self.kwargs) def status(): global I icons = ['|','/','--','\\'] print icons[I] I += 1 if I == 4: I = 0 timer = RepeatingTimer(1.0, status) timer.daemon = True # Allows program to exit if only the thread is alive timer.start() proc = subprocess.Popen([ 'python', "wait.py" ]) proc.wait() timer.cancel()This code works as I can show the fan, but with carriage return to show as follows.
| / -- \ | / -- ...What's the python code to print the characters without moving the caret position?
最满意答案
\n (新行)由print语句自动插入。 避免它的方法是用逗号结束你的语句。
如果您希望自己的风扇在线上,请使用:
print icons[I]+"\r",\r表示回车。
如果您希望您的粉丝位于非空行的末尾,请使用\b作为退格符:
print icons[I]+"\b",但要注意不要在它之后写任何风扇字符以外的任何东西。
由于print有其他一些特性,你可能想要使用kshahar建议使用sys.stdout.write() 。
\n (new line) is automatically inserted by your print statement. The way to avoid it is to end your statement with a comma.
If you want your fan to be on a line on it's own, use:
print icons[I]+"\r",\r represents the carriage return.
If you want your fan to be at the end of a non-empty line, use \b for the backspace character:
print icons[I]+"\b",but be wary of not writing anything other than the fan characters after it.
Since print has got some other peculiarities, you may want to go with kshahar suggestion of using sys.stdout.write().
更多推荐
发布评论