![](/img/trans.png)
[英]Python: How to replace tqdm progress bar by next one in nested loop?
[英]Python - How to make tqdm print one line of progress bar in shell?
我用 Python 编写了一些动画脚本,并使用 Pyinstaller 使其成为 .exe 文件。 该代码在 PyCharm 中运行良好,即仅显示单行进度条。 但是,当我打开我认为在 shell 提示符下运行的 .exe 时,它显示多行。 我使用的是 Windows 10 和 Python 3.7。 这个问题很容易在我的电脑上复制。 下面是一个例子。
import matplotlib.pyplot as plt
import matplotlib.animation as ani
import numpy as np
import pandas as pd
from tqdm import tqdm
fig = plt.figure(figsize=(20,12))
ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2, rowspan=2)
def test(i):
data = np.random.normal(0, 1, i+1)
pd.DataFrame(data).plot(kind='bar', ax=ax1)
Writer = ani.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1000)
anim = ani.FuncAnimation(fig=fig, func=test, frames=tqdm(range(10), initial=1, position=0), interval=200, blit=False)
anim.save('textmovie.mp4', writer=writer)
在我使用 Pyinstaller 生成 .exe 后,它看起来像这样。
我在这里搜索过类似的问题。 似乎人们建议使用 position=0,但这对我不起作用。 我是否遗漏了代码中的任何内容?
我知道我可以自己制作进度条而不是使用 tqdm。 但我想尽可能使用 tqdm,因为它提供了更多信息,如迭代速度和估计时间。
尝试在您对tqdm
的调用中传递file=sys.stdout
。 这是我在FuncAnimation
为帧设置的FuncAnimation
:
frames=tqdm(range(n_frames), file=sys.stdout)
,
其中n_frames
是<int>
。 记得import sys
。
更多细节:我相信tqdm()
原因是tqdm()
默认情况下有leave=True
,这意味着 bar 的每次迭代都会在写入的地方持续存在。 强制file=sys.stdout
告诉 tqdm 装饰控制台的所有内容。 它甚至会根据控制台的宽度调整栏。 如果您使用 tqdm 的电报功能,这也很有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.