我在 MacOSX 上使用功能强大的 Sublime Text 3 编辑器来运行 python 代码。我想显示一个 for 循环的进度条,以及以下命令:
sys.stdout.write('\rProgress : %03.2f %%' % (100*float(i)/(N)))
sys.flush()
没有按预期清除输出窗口中先前打印的行 (\r
),而是生成 N
行:
Progress : 0.25 %
Progress : 0.50 %
Progress : 0.75 %
Progress : 1.00 %
Progress : 1.25 %
Progress : 1.50 %
Progress : 1.75 %
Progress : 2.00 %
Progress : 2.25 %
Progress : 2.50 %
...
这不是很好读——我的结论是输出窗口可能是只读的。
有没有人建议改进 Sublime Text 中进度条的使用?
最佳答案
作为另一种替代解决方案您可以使用状态栏。当您设置状态栏消息时,先前的文本将被清除。 包控制还在安装包时使用状态栏。
例子:
import sublime, sublime_plugin
import time
class ExampleCommand(sublime_plugin.WindowCommand):
def run(self, args):
sublime.set_timeout_async(self.test,1)
def test(self):
i=80
while i <= 100:
sublime.status_message('%03.2f %%' % i)
time.sleep(0.15)
i+=0.25
sublime.status_message('100% Stackoverflow!')
关于python - 使用 Python 的 Sublime Text 中的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205245/