Dušan Atanacković
Dušan Atanacković

Reputation: 444

How to destroy widget in pyqt4 in python

I have recently switched from tkinter to pyqt4, and maybe this question is duplicate but i cant find working example/answer for it. I have , in my app, buttons that controls inputs from widgets and update DB.It works wery well but i wan't to make something like tkinter's .askyesno (or html modal) in order to ask user if he/she is sure about that command.

def modal(self):
        d = QtGui.QDialog()
        d.setModal( True )

        b1 = QtGui.QPushButton("ok",d)
        b1.move(50,50)
        b1.clicked.connect(lambda event: self.yes(d))

        d.setWindowTitle("Dialog")
        d.setWindowModality(QtCore.Qt.ApplicationModal)
        d.exec_()

        print('b1 ans', self.ans)


def yes(self, d):
    self.ans =  True
    d.setParent(None)

Now, when i click button, it calls modal function which makes QDialog with button in it.Now my problem is, since i've set setModal(True) program waits for QDialog to exit, but it exit only on small, red x/ close button, and i need QDialog to exit/ remove/ destroy whatever with button click e.g. in function 'yes'?

Is it possible (i guess it is), and how. Thanks in advance.

Upvotes: 0

Views: 702

Answers (1)

Marcus Krautwurst
Marcus Krautwurst

Reputation: 111

What you want is provided by QMessageBox. Here is a small example with a Yes, No and Cancel button that you can give custom names.

confirm = QtGui.QMessageBox()
confirm.setWindowTitle("Are you sure?")
confirm.setText("Please choose an option")

btnYes = confirm.addButton("Yes", QtGui.QMessageBox.YesRole)
btnNo = confirm.addButton("No", QtGui.QMessageBox.NoRole)
btnCancel = confirm.addButton("Cancel", QtGui.QMessageBox.RejectRole)
confirm.setDefaultButton(btnYes)
confirm = confirm.exec_()

Upvotes: 1

Related Questions