【发布时间】:2015-10-31 07:51:02
【问题描述】:
在 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"))
【问题讨论】:
-
我目前也面临这个问题。你找到解决办法了吗?
-
我找到了this pycharm support thread,这表明设置“在输出控制台中模拟终端”选项可能会显示正确的 python 回溯而不是无用的退出代码消息。
-
这能回答你的问题吗? PyQt: No error msg (traceback) on exit