Python 尝试/除非PyQt5未按预期工作
我正在写一个程序,其中一部分从文件中下载数据,特别是浮点数。我使用except来捕获将字符串转换为浮点时的错误,但当出现异常时会发生奇怪的事情。在本例中,Python 尝试/除非PyQt5未按预期工作,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,我正在写一个程序,其中一部分从文件中下载数据,特别是浮点数。我使用except来捕获将字符串转换为浮点时的错误,但当出现异常时会发生奇怪的事情。在本例中,sys.exit(1),infoBox.setText被完全省略。我不知道为什么会发生这种情况,我非常感谢您的帮助。代码如下: def on_click_load(self): with open('parameters.txt', 'r', encoding='utf-8') as file: a = file.rea
sys.exit(1)
,infoBox.setText
被完全省略。我不知道为什么会发生这种情况,我非常感谢您的帮助。代码如下:
def on_click_load(self):
with open('parameters.txt', 'r', encoding='utf-8') as file:
a = file.read()
help_tab = []
# Pipe along with O, x are special markers in file
for i in range(len(a) - 3):
if a[i] == '|' and a[i + 1] == 'O' and a[i + 2] == 'x':
while a[i + 3] != '|':
if a[i + 3].isdigit() == True:
help_tab.append(a[i + 3])
elif a[i + 3] == '.' or a[i + 3] == '-':
help_tab.append(a[i + 3])
else:
j = ''.join(help_tab)
try:
self.TabX.append(float(j))
except ValueError:
# I have problem in this section
self.infoBox.setText(
'There is an error in data in input file . The window will close in 3 seconds')
QTimer.singleShot(3000, lambda: sys.exit(1))
help_tab.clear()
i += 1
你可以试试
except Exception as e:
print (f'{type(e)}: {e}')
找出它是什么类型的错误以及错误是什么,以便您可以看到错误消息并从中进行调试
此外,您可以通过在每个部分前后添加打印语句来隔离问题所在。例如:
try:
print ('start')
self.TabX.append(float(j))
print ('append succeed ' + str(float(j))
except Exception as e:
print (f'{type(e)}: {e}')
print ('entered')
self.infoBox.setText('There is an error in data in input file . The window will close in 3 seconds')
print ('next')
QTimer.singleShot(3000, lambda: sys.exit(1))
print ('end')
我试图重现你的问题,但它对我来说是正确的,所以如果你需要帮助,你应该提供一个解决方案。在
QTimer.singleShot(3000,lambda:sys.exit(1))之后尝试添加return