我正在面對一些使用pyqt5的代碼的問題。 當我的Qt類出現問題時,控制檯不會記錄關於發生崩潰原因的任何信息。 例如,此代碼:爲什麼PyCharm中的python控制檯在使用pyqt時不顯示任何錯誤消息?
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()
是他們登錄控制檯的一些信息,而無需使用try
另一種方式except
?
正如@three_pineapples所提到的,我在'真正的'windows終端(使用c:\ anaconda3 \ python.exe)中執行腳本時遇到了錯誤,但沒有在PyCharm控制檯(當我運行腳本時) 。那麼他們是否直接在Pycharm中強制執行錯誤日誌?也許這是一個我沒有找到的選項?
您使用python.exe或python ** w **。exe來啓動代碼嗎?前者應該向終端打印例外。 –
我使用PyCharm,我選擇的解釋器是c:\ anaconda3 \ python.exe。 但它不打印任何東西。 – ymmx
但是,你說得對,當我用c:\ anaconda3 \ python.exe運行Windows CMD腳本時,我在登錄終端時出現錯誤。所以這個問題可能來自PyCharm? – ymmx