我有一个PyQt5应用程序,比方说,它可能会在出现某些错误后崩溃。
我怎样才能在下面的示例代码中实现这样的结果,以便在主窗口下降后关闭,并调用一个带有错误文本和“确定”按钮的小对话框,关闭后应用程序退出app?
import sys
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QMainWindow, QApplication, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.central_widget = QWidget(self)
self.setCentralWidget(self.central_widget)
self.layout_main_window = QVBoxLayout()
self.central_widget.setLayout(self.layout_main_window)
self.btn_crash_programm = QPushButton('Crash')
self.btn_crash_programm.clicked.connect(self.crash)
self.layout_main_window.addWidget(self.btn_crash_programm)
def crash(self):
x = 2 / 0
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle('Fusion')
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
我打算通过将这部分代码包装在 中来实现try - except,但这不起作用,块except没有执行,主窗口关闭,应用程序继续运行并加载系统。
main_window = MainWindow()
main_window.show()
作为一个选项: