PyQt dialog - How to make it quit after pressing a button?

Replace lambda: os.system(cmd) with a function/method that has multiple statements.

def buttonClicked(self):
    os.system(cmd)
    QtCore.QCoreApplication.instance().quit()

...
    btn = QtGui.QPushButton('Yes', self)     
    btn.clicked.connect(self.buttonClicked)
...

btn.clicked.connect(self.close)

That would be my suggestion


Step1: in the Main Class needs to be build a "connection":

self.ui.closeButton.clicked.connect(self.closeIt)

Step2: Creating a function like to close:

def closeIt(self): 
        self.close()

I named to "closeIt" on purpose because if you name it "close" a conflict will occur.

This solution has the advantage if the created GUI is a plugin for another program (like in my case QGIS), only the active GUI will be closed and not the whole program.


Subclass QDialog() and then close it using your object.

class Dialog(QDialog):
    """
        Subclassing QDialog class.
    """
    def __init__(self):
        QDialog.__init__(self)

    def close_clicked(self):
        self.close()

In your main function write following code

dialogbox = Dialog()  # we subclasses QDialog into Dialog
b1= QPushButton("Close",dialogbox)
b1.clicked.connect(dialogbox.close_clicked)
dialogbox.exec_()