[Python] Alternative to "QMessageBox.Yes" for PyQt6

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to port over a script of mine from PyQt5 to PyQt6. I have figured out how to port most of the things thanks to this answer, however, I have run into an issue.

    I have figured out that PyQt6 uses QtWidgets.QMessageBox.StandardButtons.Yes instead of PyQt5's QtWidgets.QMessageBox.Yes.

    However, when checking if the user pressed "Yes" after a QMessageBox opens, replacing QtWidgets.QMessageBox.Yes with QtWidgets.QMessageBox.StandardButtons.Yes doesn't work (check the examples below).


    Examples:

    PyQt5:

    reply = QtWidgets.QMessageBox()
    reply.setText("Some random text.")
    reply.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)

    x = reply.exec_()

    if x == QtWidgets.QMessageBox.Yes:
    print("Hello!")


    Printing "Hello!" here works normally. (16384 == 16384)

    PyQt6:

    reply = QtWidgets.QMessageBox()
    reply.setText("Some random text.")
    reply.setStandardButtons(QtWidgets.QMessageBox.StandardButtons.Yes |
    QtWidgets.QMessageBox.StandardButtons.No)

    x = reply.exec()

    if x == QtWidgets.QMessageBox.StandardButtons.Yes:
    print("Hello!")


    "Hello!" here doesn't print at all. (16384 != StandardButtons.yes)


    I know I could just do:

    x = reply.exec()

    if x == 16384:
    print("Hello!")


    because, after pressing "Yes", the QMessageBox equals to 16384 (see this), but I'd like to not use that approach, and rather use something like the PyQt5 example.

    Continue reading...

Compartilhe esta Página