简体   繁体   English

PyQt-单击“退出”按钮时显示消息框

[英]PyQt - Showing a MessageBox when clicking a 'Quit' button

I have an application which inherits form QtGui.QMainWindow and which redefines the closeEvent to show a MessageBox. 我有一个应用程序,它继承了QtGui.QMainWindow的形式,并重新定义了closeEvent以显示一个MessageBox。

def closeEvent(self, event):

    reply = QtGui.QMessageBox.question(
                              self,
                              'Quit',
                              'Are you sure you want to quit?',
                              QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
                              QtGui.QMessageBox.Yes)

    if reply == QtGui.QMessageBox.Yes:
        event.accept()
    else:
        event.ignore()

This MessageBox shows up when I click on the 'X' in the window. 当我在窗口中单击“ X”时,将显示此消息框。 The Application also has a 'Quit' button. 该应用程序还具有“退出”按钮。 I tried to connect the button to the redefinition of the closeEvent, so when I click the button the MessageBox shows up. 我试图将按钮连接到closeEvent的重新定义,所以当我单击按钮时,将显示MessageBox。 But when I confirm that I want to quit, I just get back to to my Application. 但是,当我确认要退出时,就回到我的申请表。

def create_components(self):

    self.button = QtGui.QPushButton('Quit')
    self.button.clicked.connect(self.button_quit)

def button_quit(self):

    self.status_bar.showMessage('Leaving Application')
    # QtCore.QCoreApplication.instance().quit()
    self.closeEvent(QtGui.QCloseEvent())

The 'create_components' method is called in the init . init中调用'create_components'方法。

Call self.close() and closeEvent will be emitted by Qt 调用self.close()closeEvent将由Qt发出

def button_quit(self):
    self.status_bar.showMessage('Leaving Application')
    self.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM