首页 > 解决方案 > 如何使用 tkinter python 在控制台底部显示进度条

问题描述

我想始终在底部显示我的进度条,我的“for”循环包裹在 tqdm 周围,并且正在调用一些函数,该函数每次都需要将近 45 秒,并为某些代码打印成功和失败消息。我正确地获得了进度条,但是对于每个循环,例如如果我的“for”循环迭代 5 次,那么每次调用都会显示 5 个进度条。我的控制台看起来像这样 -

  0%|                                                                                                            | 0/4 [00:00<?, ?it/s]
Processing ---ASHISH KUMAR SINGH ---
ASHISH KUMAR SINGH is registered already
Failure --- ASHISH KUMAR SINGH --- due to Message: no such window: window was already closed
  (Session info: chrome=86.0.4240.111)

 25%|█████████████████████████                                                                           | 1/4 [00:16<00:49, 16.52s/it]
Processing ---ANJANI LAL ---
ANJANI LAL is registered already
Failure --- ANJANI LAL --- due to Message: no such window: window was already closed
  (Session info: chrome=86.0.4240.111)

 50%|██████████████████████████████████████████████████                                                  | 2/4 [00:25<00:28, 14.19s/it]
Processing ---SORAV SAINI ---
SORAV SAINI is registered already
Failure --- SORAV SAINI --- due to Message: no such window: target window already closed
from unknown error: web view not found
  (Session info: chrome=86.0.4240.111)

 75%|███████████████████████████████████████████████████████████████████████████

我怎样才能更新底部的栏,例如我希望一个栏保留在底部并为每个循环动态更新这样->

Processing ---ANJANI LAL ---
ANJANI LAL is registered already
Failure --- ANJANI LAL --- due to Message: no such window: window was already closed
  (Session info: chrome=86.0.4240.111)

Processing ---SORAV SAINI ---
SORAV SAINI is registered already
Failure --- SORAV SAINI --- due to Message: no such window: target window already closed
from unknown error: web view not found
  (Session info: chrome=86.0.4240.111)
100%|████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4

我希望这个栏保持在底部并动态更新我怎样才能做到这一点?谢谢

标签: pythontkintercmd

解决方案


使用 tqdm.write() 代替打印功能效果很好。现在我得到了正确的输出,因为我想成为感谢@acw1668


推荐阅读