如何创建旋转的命令行光标?
有没有办法使用 Python 在终端中打印旋转光标?
Is there a way to print a spinning cursor in a terminal using Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有没有办法使用 Python 在终端中打印旋转光标?
Is there a way to print a spinning cursor in a terminal using Python?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(26)
像这样,假设你的终端处理 \b
Something like this, assuming your terminal handles \b
易于使用的 API(这将在单独的线程中运行微调器):
现在在代码中任意位置的
with
块中使用它:Easy to use API (this will run the spinner in a separate thread):
Now use it in a
with
block anywhere in the code:一个不错的 Pythonic 方法是使用 itertools.cycle:
此外,您可能希望在长函数调用期间使用线程来显示微调器,如 http://www.interclasse.com/scripts/spin.php
A nice pythonic way is to use itertools.cycle:
Also, you might want to use threading to display the spinner during a long function call, as in http://www.interclasse.com/scripts/spin.php
为了完整起见,我想添加很棒的包 光环。它提供了大量预设旋转器和更高级别的自定义选项。
从他们的自述文件中摘录
或者,您可以将 halo 与 Python 的 with 语句一起使用:
最后,您可以使用 halo 作为装饰器:
For completeness I want to add the great package halo. It offers a lot of preset spinners and higher level customization options.
Extract from their readme
Alternatively, you can use halo with Python's with statement:
Finally, you can use halo as a decorator:
解决方案:
关键是使用退格字符“\b”并刷新标准输出。
A solution:
The key is to use the backspace character '\b' and flush stdout.
来自 @Victor Moyseenko 的改进版本
因为原始版本几乎没有问题
上面 Spinner 类的使用示例:
将代码上传到 https:// github.com/Tagar/stuff/blob/master/spinner.py
Improved version from @Victor Moyseenko
as the original version had few issues
example of usage of the Spinner class above:
uploaded code to https://github.com/Tagar/stuff/blob/master/spinner.py
不错,简单,干净……
Nice, simple, and clean...
当然,这是可能的。这只是在四个字符之间打印退格字符 (
\b
) 的问题,这会使“光标”看起来像在旋转 (-
,\
、|
、/
)。Sure, it's possible. It's just a question of printing the backspace character (
\b
) in between the four characters that would make the "cursor" look like it's spinning (-
,\
,|
,/
).我在 GitHub 上找到了 py-spin 包。它有许多漂亮的旋转款式。这里有一些关于如何使用的示例,
Spin1
是\-/
样式:也可以手动控制旋转:
下面的 gif 中的其他样式。
I have found py-spin package on GitHub. It has many nice spinning Styles. Here are some sample about how to use,
Spin1
is the\-/
style:It is also possible to control the spin manualy:
Other styles in the below gif.
获取出色的
progressbar
模块 - http://code.google.com /p/python-进度条/使用旋转标记。
Grab the awesome
progressbar
module - http://code.google.com/p/python-progressbar/use
RotatingMarker
.我构建了一个通用的单例,由整个应用程序共享
I built a generic Singleton, shared by the entire application
您可以编写
'\r\033[K'
来清除当前行。以下是@nos 修改的示例。对于任何对nodejs感兴趣的人,我还写了一个nodejs示例。
You can write
'\r\033[K'
to clear the current line. And the following is a example modified from @nos.For anyone who interested in nodejs, I also write a nodejs example.
curses 模块。 我会看看 addstr() 和 addch( ) 函数。虽然从未使用过它。
curses module. i'd have a look at the addstr() and addch() functions. Never used it though.
对于更高级的控制台操作,在 unix 上,您可以使用 curses python 模块 ,在 Windows 上,您可以使用 WConio 它提供了与 curses 库等效的功能。
For more advanced console manipulations, on unix you can use the curses python module, and on windows, you can use WConio which provides equivalent functionality of the curses library.
注意事项:
根据我的经验,这并不适用于所有终端。在 Unix/Linux 下执行此操作的更强大方法是使用 curses 模块,该模块在 Windows 下不起作用。
您可能希望通过后台进行的实际处理来减慢速度。
CAVEATS:
In my experience this doesn't work in all terminals. A more robust way to do this under Unix/Linux, be it more complicated is to use the curses module, which doesn't work under Windows.
You probably want to slow it down some how with actual processing that is going on in the background.
开始吧——简单明了。
Here ya go - simple and clear.
粗略但简单的解决方案:
有明显的局限性,但同样是粗略的。
Crude but simple solution:
There are obvious limitations, but again, crude.
我提出了一个使用装饰器的解决方案
简单用法
I propose a solution using decorators
Simple usage
这也可以是使用带有参数的函数的另一种解决方案。
This can be also another solution using a function with a parameter.
大约一周前,我刚刚开始使用 python,并发现了这篇文章。我将我在这里找到的一些内容与我在其他地方学到的有关线程和队列的内容结合起来,以提供我认为更好的实现。在我的解决方案中,写入屏幕是由检查队列内容的线程处理的。如果该队列有内容,游标旋转线程就会知道停止。另一方面,游标旋转线程使用队列作为锁,因此打印线程知道在旋转器代码的完整传递完成之前不要打印。这可以防止竞争条件和人们用来保持控制台干净的大量多余代码。
见下文:
说了我所说的一切并写下了我所写的一切,我只做了一周的Python工作。如果有更干净的方法来做到这一点,或者我错过了一些我很想学习的最佳实践。谢谢。
I just started with python about a week ago and found this posting. I've combined a bit of what I found here with stuff I learned about threads and queues elsewhere to provide a much better implementation in my opinion. In my solution, writing to the screen is handled by a thread that checks a queue for content. If that queue has content, the cursor spinning thread knows to stop. On the flipside, the cursor spinning thread uses a queue as a lock so the printing thread knows not to print until a full pass of the spinner code is complete. This prevents race conditions and a lot of excess code people are using to keep the console clean.
See below:
Having said all I said and written all I've written, I've only been doing python stuff for a week. If there are cleaner ways of doing this or I missed some best practices I'd love to learn. Thanks.
超级简单的事情:
如果您知道最终指标,那么这也会打印进度和 eta。
Something super simple:
If you know the final metric then this will print the progress and eta as well.
我们开始吧,更简单、更概念化:
在文件中执行代码,否则它将无法工作(不要尝试在 Python 终端中执行代码)
输出:
time.sleep ()
用于加载时间。pattern
列表。示例:
Here we go, much simplier and more conceptual:
Execute your code within the file otherwise it won't work (don't try to execute code in Python terminal)
Output:
time.sleep()
for loading time.pattern
list for different graphics.Example:
Output:
In line 6:
*L = " Loading... " + str(j) + "%" + str(i)
Here
j
is integer (number)0
to100
, so for printing usingsys.std.write()
it is necessary to convert integer to string so we calledstr(j)
.alive-progress 库有很多选项。
您可以通过从终端运行以下命令来尝试实时演示:
python -m alive_progress.tools.demo
The alive-progress library has a lot of options.
You can try a live demo by running the following command from a terminal:
python -m alive_progress.tools.demo
如果你想要 python 文本旋转器,你可以看图片
简单:
if you wanna python text spinner you can look picture
Simple:
这是最简单的 python 加载旋转器:
输出:
Here is the simplest loading spinner for python:
Output: