3

I am having an error when I exit my GUI:

Error: "python.exe has stopped working"

This happens when I exit using the topmenu and toolbar exit option. And it also happens when I close the program on the "X" on the top right.

However when I comment the line:

self.mainToolBar.addAction(exitAction)

The "X" on the top right wont give this error.

For the exit option on the toolbar and top menu I am using this:

exitAction.triggered.connect(qApp.quit)

Follow the code:

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()    

How do I fix this?

4

2 回答 2

4

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

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

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

查看文档

于 2014-05-19T10:52:06.367 回答
0

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

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

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

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

我希望这有帮助。

于 2013-11-26T13:05:50.570 回答