Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pyqt

pyqt5

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. This doesn't happen when the error is in __init__.
It makes it very difficult to debug. How do I fix this?

This widget is generated by Qt Designer

enter image description here

For example, if I wrote button.setText('a'+1) when clicked on the button:

# -*- 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_())

enter image description here

It works fine in windows console:

enter image description here

test.py(generated by 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"))
like image 406
liyuanhe211 Avatar asked Aug 07 '15 19:08

liyuanhe211


People also ask

Can I use PyQt5 in PyCharm?

Once qt5-designer is installed, you can configure it in PyCharm.

How do I add Qt Designer to PyCharm?

Go to File > Settings > tools > PyCharm External Tools , and include the following information to add them to your project. Later, you will be able to right click a . ui file, and select Qt Designer , pyside6-uic , or any tool that you configured this way.

What is PyQt5 library in Python?

PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library. A GUI application consists of Front-end and Back-end.


1 Answers

As has been pointed out in the comments and indeed suggested on this pycharm thread:

In your run configuration, enable the option 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

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.

like image 165
Abby Avatar answered Oct 17 '22 04:10

Abby