在Pycharm 4.5.2中,如果我在PyQt5
个插槽中出错,当调用插槽时,Pycharm只显示Process finished with exit code 1
,但不会显示错误发生的位置和原因。如果错误在__init__
中,则不会发生这种情况
这使得调试非常困难。我该如何解决这个问题?
此小部件由Qt Designer
生成例如,如果我在点击按钮时写了button.setText('a'+1)
:
# -*- coding: utf-8 -*-
import sys
from PyQt5 import Qt
from test import Ui_Form
Application = Qt.QApplication(sys.argv)
class myWidget(Qt.QWidget):
def __init__(self):
super(myWidget, self).__init__()
self.main = Ui_Form()
self.main.setupUi(self)
# self.main.pushButton.setText('a'+1)
# prints `TypeError: Can't convert 'int' object to str implicitly ` normally
self.show()
self.main.pushButton.clicked.connect(self.show_error)
def show_error(self):
self.main.pushButton.setText('a'+1)
# only print "Process finished with exit code 1" when clicked on the button, and crash.
my_Qt_Program = myWidget()
my_Qt_Program.show()
sys.exit(Application.exec_())
在Windows控制台中工作正常:
test.py(由Qt Designer生成):
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.5
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(115, 58)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Show \'a\' +1"))
答案 0 :(得分:0)
也许不是您想要的,但这对我有用:
import logging
def main():
x = 1/0
if __name__ == '__main__':
logging.basicConfig(level='INFO')
main()
编辑:如果您只有一个文件,则上述方法有效,但无法解决问题的根源 PyQt: No error msg (traceback) on exit
可在此处找到更好的答案答案 1 :(得分:0)
正如评论中指出的,并确实对此pycharm thread提出了建议:
在运行配置中,启用选项Emulate terminal in output console
。您可以在“执行”部分中找到它。
在我的计算机(Windows 10,PyCharm Professional 2018.3.1)上,这将单击show 'a' + 1
按钮时的行为从退出代码(-1073740791 (0xC0000409)
)退出显示为
Traceback (most recent call last):
File "path/to/file/so.py", line 25, in show_error
self.main.pushButton.setText('a' + 1)
TypeError: can only concatenate str (not "int") to str
为了运行您的示例,我必须进行更改
from PyQt5 import Qt
进入
from PyQt5.QtWidgets import QApplication, QWidget
并将相应的Qt.Q...
调用更改为Q...
,但这可能取决于我的设置。