Switching Buttons in QMessageBox

Question:

I have this code:

def initUI(self):
    mainLayout = QVBoxLayout()

    # Temporary: Dialog Testing Button #
    self.buttonTest = button('Dialog Testing')
    self.buttonTest.clicked.connect(self.TestFile)
    mainLayout.addWidget(self.buttonTest)

    self.setLayout(mainLayout)

# Connects to dialogTesting Button
def dialogMessage(self, message):
    dialog = QMessageBox(self)
    dialog.setWindowTitle('Sample Text')
    dialog.setText(message)

    dialog.show()

# Connects with dialogMessage class.
def TestFile(self):
    self.dialogMessage("When will non-OK buttons appear?")
    return

I get results like this:

App with Popup
App with Button

How can I change what buttons appear in the popup?

Asked By: Fumble

||

Answers:

You can change the buttons by using an argument for the buttons parameter when you instantiate a QMessageBox. You use the Standard Button constants. Here is a silly example:

dialog = QMessageBox(self, buttons=QMessageBox.Ok+QMessageBox.Save+QMessageBox.Yes )

If you don’t like the choices for standard buttons, you can make your own using addButton. This answer shows how to use addButton.

Answered By: bfris
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.