Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Well, I'm writing a small PyQt4 app, it's just a single Yes/No dialog which has to execute an external command (e.g. 'eject /dev/sr0') and quit.

The app runs, it executes the command after pressing the "Yes" button, but I cannot make the dialog itself exit when executing the command.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import os
import subprocess
from PyQt4 import QtGui
from PyQt4 import QtCore
from subprocess import call
cmd = 'eject /dev/sr0'

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        btn = QtGui.QPushButton('Yes', self)     
        btn.clicked.connect(lambda: os.system(cmd))
        btn.resize(180, 40)
        btn.move(20, 35)       

        qbtn = QtGui.QPushButton('No', self)
        qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        qbtn.resize(180, 40)
        qbtn.move(20, 80) 

        self.setWindowTitle('Test')    
        self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Here is my code. When I click "Yes", it calls the 'eject /dev/sr0' command properly, but after that the dialog is still visible. I have to click "No" to close the app I would like it to close automatically when the command is executed. What should I add/modify?

like image 838
Laszlo Meller Avatar asked Aug 02 '12 07:08

Laszlo Meller


1 Answers

btn.clicked.connect(self.close)

That would be my suggestion

like image 75
Manjabes Avatar answered Oct 17 '22 03:10

Manjabes