鼠标/键盘意外输入时,tqdm在Windows控制台上崩溃 [英] tqdm crashes on Windows console upon accidental mouse/keyboard input
问题描述
在Windows上运行任何使用tqdm
进度条的应用程序都变得非常头疼.
Running any application on Windows that make use of tqdm
progress bars has become a tremendous headache.
我不清楚这是否是Windows错误,但该错误很容易重现.在 cmd.exe 或 Powershell 上运行以下代码:
It's not clear to me if this is a Windows fault or not, but the bug is easily reproducible. Run the following code on cmd.exe or Powershell:
from tqdm import *
import time
counter = 1000
for i in tqdm(range(counter)):
time.sleep(.01)
在进度栏增加以触发崩溃时,您可以执行以下操作:
You can do the following while the progress bar is increasing to trigger the crash:
- 使用鼠标左键按钮选择窗口上的几个字符(如下面的屏幕快照所示,即使空格也可以),然后右键单击或按任意键以使应用程序崩溃:
- Use the left-mouse button to select a few characters on the window (even blank spaces will do, as the screenshot below demonstrates) and then either right-click or press any key to crash the application:
错误消息显示:
Traceback (most recent call last):
File "tqdmTest.py", line 5, in <module>
for i in tqdm(range(counter)):
File "C:\Users\brcod\AppData\Roaming\Python\Python34\site-packages\tqdm\_tqdm.py", line 979, in __iter__
sp(self.__repr__())
File "C:\Users\brcod\AppData\Roaming\Python\Python34\site-packages\tqdm\_tqdm.py", line 241, in print_status
fp_write('\r' + s + (' ' * max(last_len[0] - len_s, 0)))
File "C:\Users\brcod\AppData\Roaming\Python\Python34\site-packages\tqdm\_tqdm.py", line 234, in fp_write
fp.write(_unicode(s))
OSError: raw write() returned invalid length 306 (should have been between 0 and 153)
我在 Windows 10 和 tqdm 4.19.5 上使用 Python 3.4.4 .
这很烦人,因为当我试图单击鼠标以使窗口聚焦时,鼠标意外地在窗口中选择了几个字符.
This is very annoying because the mouse accidentally selects a few characters in the window when I'm just trying to click on it to focus the window.
任何人都可以澄清为什么会发生这种情况吗?是否有解决此问题的适当方法?
Can anyone clarify why this happens? Is there a proper workaround for this problem?
推荐答案
解决方案1 :此问题最简单的解决方案是在以下属性中禁用 QuickEdit Mode cmd.exe 窗口,以防止鼠标单击意外选择并粘贴文本:
Solution 1: the most simple solution for this problem is to disable QuickEdit Mode in the properties of the cmd.exe window to prevent mouse clicks from accidentally selecting and pasting text:
解决方案2 :这也可以通过编程方式进行处理.只需重写循环以捕获异常,并在发生异常时pass
即可:
Solution 2: this can also be handled programmatically. Just rewrite the loop to catch the exception and pass
it when it happens:
from tqdm import *
import time
maxCount = 1000
pbar = tqdm(total = maxCount)
for i in range(maxCount+1):
try:
pbar.update(i - pbar.n)
except OSError as e:
pass
time.sleep(.01)
pbar.close()
这篇关于鼠标/键盘意外输入时,tqdm在Windows控制台上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!