문제

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?

도움이 되었습니까?

해결책 2

I've done the same like this: QtGui.qApp.quit() , in your case:

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

If error still exists, try overriding closeEvent like this:

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

I hope this helps.

다른 팁

I am relatively new to PyQt but wouldn't this work:

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

Just tested on my machine and it exits cleanly.

See docs

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top