擅长:python、mysql、java
<p>尝试使用<code>tqdm.tqdm.write</code>代替标准<code>print()</code></p>
<p>这将打印在进度条上方,并将进度条移动到下方一行。</p>
<p>我使用下面的代码对此进行了测试,按<code>space</code>将打印到stdout中,但不会中断循环。
还不完全清楚您要实现什么,因为您的<code>interrupt()</code>函数只检查提供的字符串的类型。<a href="https://docs.python.org/3/library/functions.html#type" rel="nofollow noreferrer">type() built-in function</a></p>
<pre><code>import tqdm
import msvcrt
from time import sleep
def transfer():
for i in tqdm.tqdm(range(1000), desc="Transfer progress", ncols=100, bar_format='{l_bar}{bar}|'):
sleep(.1)
if msvcrt.kbhit():
if msvcrt.getwche() == ' ':
interrupt()
# break
def interrupt():
tqdm.tqdm.write("File transfer interrupted, to restart the transfer, type 'restart'", end="")
transfer()
</code></pre>
<p>编辑:包括Paul Netherwood指出的<code>tqdm.write()</code>的<code>end</code>参数
<a href="https://tqdm.github.io/docs/tqdm/#write" rel="nofollow noreferrer">tqdm.tqdm.write()</a></p>