用于设置旋转风扇动画的Python代码(Python code to animate a rotating fan to appear in place)

编程入门 行业动态 更新时间:2024-10-26 01:17:38
用于设置旋转风扇动画的Python代码(Python code to animate a rotating fan to appear in place)

我需要根据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().

更多推荐

本文发布于:2023-07-30 01:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:风扇   代码   动画   Python   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!