PyQT Popup Message Not Working With Frame Exit Button - Homework Question Answered!

by: superpower!, 9 years ago

Last edited: 9 years ago

On PyQT Tutorial 7, you assigned a homework question regarding popup box not executing on frame exit button click. The new code starts at #New Code Starts Here, below. Don't forget to get the required image files before running. Feel free to comment.


import sys
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PyQT tuts!")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))

        extractAction = QtGui.QAction("&GET TO THE CHOPPAH!!!", self)
        extractAction.setShortcut("Ctrl+Q")
        extractAction.setStatusTip('Leave The App')
        extractAction.triggered.connect(self.close_application)

        self.statusBar()

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(extractAction)
        

        self.home()

    def home(self):
        btn = QtGui.QPushButton("Quit", self)
        btn.clicked.connect(self.close_application)
        btn.resize(btn.minimumSizeHint())
        btn.move(0,100)

        extractAction = QtGui.QAction(QtGui.QIcon('todachoppa.png'), 'Flee the Scene', self)
        extractAction.triggered.connect(self.close_application)
        
        self.toolBar = self.addToolBar("Extraction")
        self.toolBar.addAction(extractAction)

        self.show()

    def close_application(self):
        choice = QtGui.QMessageBox.question(self, 'Extract!',
                                            "Get into the chopper?",
                                            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        if choice == QtGui.QMessageBox.Yes:
            print("Extracting Naaaaaaoooww!!!!")
            sys.exit()
        else:
            pass
    #New Code Starts Here
    def closeEvent(self,event):
        if type(event) == QtGui.QCloseEvent:
            event.ignore()
            self.close_application()

        else:
            event.accept()
    #New Code Ends Here
    
def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())


run()




You must be logged in to post. Please login or register an account.



Nice job, how did you go about solving it?

-Harrison 9 years ago

You must be logged in to post. Please login or register an account.


Well, I looked it up on stackoverflow (research, not cheating) and found this:

http://stackoverflow.com/questions/9249500/pyside-pyqt-detect-if-user-trying-to-close-window

When you assigned this as homework, I had to take the challenge!

-superpower! 9 years ago

You must be logged in to post. Please login or register an account.


Nice, that's how you're supposed to learn new things, definitely not cheating!

-Harrison 9 years ago

You must be logged in to post. Please login or register an account.