【发布时间】:2017-07-26 01:56:41
【问题描述】:
我的一些使用 pyqt5 的代码遇到了一些问题。 当我的 Qt 类出现问题时,控制台不会记录有关崩溃发生原因的任何信息。 例如使用此代码:
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()
由于b 在TestForCrash 函数中未知,Qt 窗口刚刚退出,但控制台中没有任何内容。我想知道他们是否是一种强制控制台自动打印一些正在发生的事情的线索的方法。
现在我使用try except 来解决这个问题,但我不太喜欢这个想法:
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()
他们是另一种在控制台中记录一些信息而不使用tryexcept的方法吗?
正如@three_pineapples 所提到的,当我在“真实”Windows 终端(使用 c:\anaconda3\python.exe)中执行脚本时出现错误,但在 PyCharm 控制台中(当我运行脚本时)没有。那么他们是一种直接在 Pycharm 中强制错误日志的方法吗?也许这是我还没有找到的选项?
【问题讨论】:
-
您是使用python.exe 还是pythonw.exe 来启动代码?前者应该将异常打印到终端。
-
我正在使用 PyCharm,我选择的解释器是 c:\anaconda3\python.exe。但它不打印任何东西。
-
但是,你是对的,当我使用带有 c:\anaconda3\python.exe 的 windows CMD 运行脚本时,我在终端中登录错误。那么问题可能来自 PyCharm?
标签: python console pyqt pycharm message