1

I'm Using PyQt5 and I've created a QMessagebox and I want to print something if the Yes Button is clicked. Here's my code

self.messageBox = QMessageBox()
self.messageBox.setText("Are You Sure with Left Edge You Chosed?")
self.messageBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
if self.messageBox == QMessageBox.Yes:
    self.confirmation = 1
    print("Yess Clicked")
else:
    self.confirmation = 0
self.messageBox.exec()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
M.H.Muhamadi
  • 139
  • 2
  • 8

1 Answers1

5

You are comparing the QMessageBox widget with QMessageBox.Yes that makes no sense. If you want to get the standardButton associated with the button pressed then you must use the standardButton() and clickedButton() method:

self.messageBox = QMessageBox()
self.messageBox.setText("Are You Sure with Left Edge You Chosed?")
self.messageBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
self.messageBox.exec_()

if self.messageBox.standardButton(self.messageBox.clickedButton()) == QMessageBox.Yes:
    self.confirmation = 1
    print("Yess Clicked")
else:
    self.confirmation = 0
eyllanesc
  • 235,170
  • 19
  • 170
  • 241