简体   繁体   English

Pycharm没有显示`PyQt5`程序的错误信息(例如`TypeError`)

[英]Pycharm doesn't show error information for `PyQt5` program (e.g. `TypeError`)

In Pycharm 4.5.2, if I had an error in PyQt5 slots, when the slots was called, Pycharm only shows Process finished with exit code 1 , but not where and why the error happends. 在Pycharm 4.5.2中,如果我在PyQt5插槽中有错误,当PyQt5插槽时,Pycharm只显示Process finished with exit code 1 ,但不显示错误发生的位置和原因。 This doesn't happen when the error is in __init__ . 当错误在__init__时,不会发生这种情况。
It makes it very difficult to debug. 这使得调试非常困难。 How do I fix this? 我该如何解决?

This widget is generated by Qt Designer 此小组件由Qt Designer生成

在此输入图像描述

For example, if I wrote button.setText('a'+1) when clicked on the button: 例如,如果我在单击按钮时写了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_())

在此输入图像描述

It works fine in windows console: 它在Windows控制台中工作正常:

在此输入图像描述

test.py(generated by Qt Designer): 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"))

Perhaps not exactly what you want but this works for me: 也许不完全是你想要的但这对我有用:

import logging

def main():
    x = 1/0

if __name__ == '__main__':
    logging.basicConfig(level='INFO')
    main()

Edit: The above mention method works if you got a single file but it doesnt fix the root of the problem A better anwser can be found here PyQt: No error msg (traceback) on exit 编辑:上面提到的方法有效,如果你有一个文件,但它没有修复问题的根源可以在这里找到更好的anwser PyQt:退出时没有错误消息(回溯)

As has been pointed out in the comments and indeed suggested on this pycharm thread : 正如已在评论中指出并确实在此pycharm线程上建议:

In your run configuration, enable the option Emulate terminal in output console . 在运行配置中, Emulate terminal in output console启用选项Emulate terminal in output console You can find this in the Execution section. 您可以在执行部分找到它。

Running your example 运行你的榜样

On my machine (Windows 10, PyCharm Professional 2018.3.1) this changed the behaviour when clicking the show 'a' + 1 button from exiting with exit code ( -1073740791 (0xC0000409) ) to showing 在我的机器上(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

In order to run your example I had to change 为了运行你的例子,我不得不改变

from PyQt5 import Qt

into

from PyQt5.QtWidgets import QApplication, QWidget

and change the Qt.Q... calls into Q... accordingly, but this might depend on my setup. 并相应地将Qt.Q...调用更改为Q...但这可能取决于我的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM