Python版本3.6.4
以下代码在VS code和IDLE中,进度条都不能单行显示。
from time import sleep
from tqdm import tqdm
bar = tqdm(range(1,20))
for each in bar:
bar.set_description('Processing %s' %each)
sleep(1)
加入ncols参数后在VS code中可正常单行显示,而IDLE中不能。
from time import sleep
from tqdm import tqdm
bar = tqdm(range(1,20),ncols=100)
for each in bar:
bar.set_description('Processing %s' %each)
sleep(1)
tqdm的help文档对ncols的解释:
ncols : int, optional
The width of the entire output message. If specified, dynamically resizes the progressbar to stay within this bound. If unspecified, attempts to use environment width. The fallback is a meter width of 10 and no limit for the counter and statistics. If 0, will not print any meter (only stats).
ncols参数控制输出消息的宽度,如果数值过大,则会多行显示;如果为 0,则不会显示进度条。所以需要将ncols参数设置为适当的值。