在从Python创建Qt应用程序的过程中,异常的处理方式很奇怪(这里使用PyQt5创建,但是我注意到PySide和PyQt4有类似的行为)。请考虑以下脚本。这可能有点太冗长了,但我想创建一个半现实的示例,其中应用程序、主窗口和中心小部件都是单独创建的(可能是在现实应用程序中由单独的Python模块创建的)。你知道吗
from sys import argv, exit
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QVBoxLayout, \
QPushButton
def main():
print("running the main Python function")
run_app()
def run_app():
print("intialising the app")
app = QApplication(argv)
ex = MainWindow()
exit(app.exec_())
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
print("initialising the main window")
gui = GUIWidget(self)
self.setCentralWidget(gui)
self.show()
class GUIWidget(QWidget):
def __init__(self, parent=None):
super(GUIWidget, self).__init__(parent)
print("initialising the gui")
self.vbox = QVBoxLayout()
self.setLayout(self.vbox)
self.button = QPushButton("Push me to produce an error")
self.button.clicked.connect(self.raise_an_expection)
self.vbox.addWidget(self.button)
# raise Exception # nicely handled exception
def raise_an_expection(self):
raise Exception # poorly handled exception
if __name__ == '__main__':
main()
在最后一行GUIWidget.__init__()
未注释的情况下,Python会引发一个异常和Process finished with exit code 1
,正如预期的那样。你知道吗
通过此注释,将创建一个带有按钮的应用程序。按按钮会引发一个异常,正如预期的那样,但也会引发Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
,我不明白。在我的mac上,这也会导致出现Python quit unexpectedly
对话框。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐