pycharm python console不显示,为什么在使用pyqt时PyCharm中的python控制台不显示任何错误消息?...

I'm facing some issue with some of my code which use pyqt5.

When something go wrong in my Qt classes, the console doesn't log any information about why the crashes happened.

for instance with this code:

rom PyQt5.QtGui import *

from PyQt5.QtCore import *

from PyQt5.QtWidgets import *

import sys

class SurfViewer(QMainWindow):

def __init__(self, parent=None):

super(SurfViewer, self).__init__()

self.parent = parent

self.centralWidget = QWidget()

self.color = self.centralWidget.palette().color(QPalette.Background)

self.setCentralWidget(self.centralWidget)

self.plotview = QGroupBox(" ")

self.layout_plotview = QVBoxLayout()

self.Button_Crash= QPushButton('Crash!')

self.layout_plotview.addWidget(self.Button_Crash)

self.centralWidget.setLayout(self.layout_plotview)

self.Button_Crash.clicked.connect(self.TestForCrash)

def TestForCrash(self,):

a=b

return

def main():

app = QApplication(sys.argv)

ex = SurfViewer(app)

ex.show()

sys.exit(app.exec_())

if __name__ == '__main__':

main()

As b is not known in TestForCrash function, the Qt window just quits but I've got nothing in the console. I'm wondering if their is a way to force the console to automatically print some clue of what is going on.

For now I'm using a try except to go around the issue but I don't like this idea much:

from PyQt5.QtGui import *

from PyQt5.QtCore import *

from PyQt5.QtWidgets import *

import sys

class SurfViewer(QMainWindow):

def __init__(self, parent=None):

super(SurfViewer, self).__init__()

self.parent = parent

self.centralWidget = QWidget()

self.color = self.centralWidget.palette().color(QPalette.Background)

self.setCentralWidget(self.centralWidget)

self.plotview = QGroupBox(" ")

self.layout_plotview = QVBoxLayout()

self.Button_Crash= QPushButton('Crash!')

self.layout_plotview.addWidget(self.Button_Crash)

self.centralWidget.setLayout(self.layout_plotview)

self.Button_Crash.clicked.connect(self.TestForCrash)

def TestForCrash(self,):

try:

a=b

except BaseException as e:

msg = QMessageBox()

msg.setIcon(QMessageBox.Critical)

msg.setText(str(e))

msg.setStandardButtons(QMessageBox.Ok)

msg.exec_()

return

def main():

app = QApplication(sys.argv)

ex = SurfViewer(app)

ex.show()

sys.exit(app.exec_())

if __name__ == '__main__':

main()

Is their another way to log some info in the console without using a try except?

As mention by @three_pineapples, I've got errors when I execute the script in 'real' windows terminal (with c:\anaconda3\python.exe) but not in the PyCharm console (when I run the script). So is their a way to force error logs in Pycharm directly? maybe it is an option I didn't find yet?

解决方案

What you can do is redefine the exception hook sys.excepthook. To quote from the documentation:

When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object. In an interactive session this happens just before control is returned to the prompt; in a Python program this happens just before the program exits. The handling of such top-level exceptions can be customized by assigning another three-argument function to sys.excepthook.

Your custom function could display, for example, a QMessagebox. This is done in the function catch_exceptions() in the following example. That function also calls the old exception hook (stored in old_hook) so that the normal path of handling exceptions is followed in addition to the message box.

import sys

from PyQt5 import QtWidgets

def catch_exceptions(t, val, tb):

QtWidgets.QMessageBox.critical(None,

"An exception was raised",

"Exception type: {}".format(t))

old_hook(t, val, tb)

old_hook = sys.excepthook

sys.excepthook = catch_exceptions

def main():

app = QtWidgets.QApplication(sys.argv)

raise RuntimeError

if __name__ == "__main__":

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值