【问题标题】:PyQt 5 error on exitPyQt 5 退出时出错
【发布时间】:2013-11-20 17:12:05
【问题描述】:

退出 GUI 时出现错误:

错误:“python.exe 已停止工作”

当我使用顶部菜单和工具栏退出选项退出时会发生这种情况。当我关闭右上角的“X”上的程序时也会发生这种情况。

但是,当我评论该行时:

self.mainToolBar.addAction(exitAction)

右上角的“X”不会出现此错误。

对于工具栏和顶部菜单上的退出选项,我使用的是:

exitAction.triggered.connect(qApp.quit)

关注代码:

class Example(QMainWindow):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):               
        self.topmenu()
        self.toolbar()

        self.resize(800, 600)    
        self.setWindowTitle('Example')
        self.setWindowIcon(QtGui.QIcon('test.gif'))    
        self.show()

    def topmenu(self):
        #Buttons
        exitAction = QAction(QtGui.QIcon('plus.gif'), '&Exit', self)        
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(qApp.quit)

        #Create MenuBar
        menubar = self.menuBar()
        #Add options
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)

    def toolbar(self):
        exitAction = QAction(QtGui.QIcon('plus.gif'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setToolTip("Exit")
        exitAction.triggered.connect(qApp.quit)

        self.mainToolBar = QToolBar(self)
        self.mainToolBar.setObjectName("mainToolBar")
        self.addToolBar(Qt.LeftToolBarArea, self.mainToolBar)
        # Line is giving the stop problem
        self.mainToolBar.addAction(exitAction)

    def main():    
        app = QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())       

    if __name__ == '__main__':
        main()    

我该如何解决这个问题?

【问题讨论】:

  • 你不应该在某个地方定义qApp吗?

标签: python qt pyqt pyqt5


【解决方案1】:

我对 PyQt 比较陌生,但是这行不通:

exitAction.triggered.connect(self.close) #Fires closeEvent()

刚刚在我的机器上测试,它干净地退出了。

见docs

【讨论】:

    【解决方案2】:

    我也做了同样的事情:QtGui.qApp.quit(),在你的情况下:

    exitAction.triggered.connect(QtGui.qApp.quit())
    

    如果错误仍然存​​在,请尝试像这样覆盖closeEvent:

    def closeEvent(self, event):
        QtGui.qApp.quit()
        event.ignore()
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-02-21
      • 1970-01-01
      • 2017-04-14
      • 2016-02-17
      • 1970-01-01
      相关资源
      最近更新 更多