gpt4 book ai didi

python - Pycharm 不显示 `PyQt5` 程序的错误信息(例如 `TypeError` )

转载 作者:太空狗 更新时间:2023-10-30 00:19:24 25 4
gpt4 key购买 nike

在 Pycharm 4.5.2 中,如果我在 PyQt5 插槽中出错,当调用插槽时,Pycharm 仅显示 Process finished with exit code 1,但不会显示错误发生的位置和原因。当错误在 __init__ 中时,这不会发生。
这使得调试变得非常困难。我该如何解决这个问题?

这个小部件是由 Qt Designer 生成的

enter image description here

例如,如果我在单击按钮时写了 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_())

enter image description here

它在 Windows 控制台中运行良好:

enter image description here

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"))

最佳答案

正如评论中所指出的,并且确实在这个 pycharm thread 上提出了建议:

在您的运行配置中,启用选项在输出控制台中模拟终端。您可以在执行部分找到它。

运行你的例子

在我的机器(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...,但这可能取决于我的设置。

关于python - Pycharm 不显示 `PyQt5` 程序的错误信息(例如 `TypeError` ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31884991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com