【问题标题】:How to get tqdm to print to the same line regardless of terminal window size?无论终端窗口大小如何,如何让 tqdm 打印到同一行?
【发布时间】:2019-12-16 06:50:57
【问题描述】:

我正在使用 tqdm 绘制进度条,并希望 tqdm 覆盖终端中的同一行,无论窗口大小如何。考虑以下代码:

from tqdm import trange
from time import sleep
t = trange(100, desc='Bar desc', leave=True)
for i in t:
    t.set_description("Bar desc (file %i)" % i)
    t.refresh() # to show immediately the update
    sleep(0.01)

当我的终端窗口的宽度大于 "Bar desc (file %i)" 时,tqdm 会按照我的意愿打印进度条。但是,如果我减小终端窗口的宽度,tqdm 将打印为两行。每个 tqdm 更新都会打印到一个新行。即使我调整终端大小,有什么办法可以让 tqdm 打印到相同的两行?

我在 Ubuntu 中使用 bash 终端。

【问题讨论】:

    标签: python bash tqdm


    【解决方案1】:

    有多种选择:

    查询终端宽度并相应地修剪你的栏linewidth=$(($(tput cols) - 1)) 要么禁用换行

    禁用换行: 诅咒tput rmam ANSI: '\x1B[?7l`

    启用换行: ncursestput smam ANSI: '\x1B[?7h`

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-15
      • 2021-07-01
      • 1970-01-01
      • 2014-11-02
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      相关资源
      最近更新 更多