Reputation: 8029
In the context of a parent class like a main window, it is easy to pop up a dialog like a message box:
QMessageBox.information(self, "Title", "Here is your informative message")
This pops up the message I want, with parent being self
, the widget already open. But what if I want to show such a dialog in the middle of a Python program all by itself (say, to tell them their program has finished running, or whatever), without invoking a parent class?
I tried the following, and the dialog shows, but when I click OK my system hangs for a few seconds, no message is printed, and my Python kernel restarts (with no error message):
import sys
from PyQt5.QtWidgets import QApplication, QMessageBox
app = QApplication(sys.argv)
mess = QMessageBox()
mess.setText("Here is your message")
mess.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
returnValue = mess.exec_()
if returnValue == QMessageBox.Ok:
print("Clicked OK!")
elif returnValue == QMessageBox.Cancel:
print("Cancelled?!")
print("\n\nNow we are done")
When I click Cancel, it seems to work fine. I am not sure what Qt rules I am breaking here. I could just roll my own little popup using a QWidget
I suppose.
I am in pyqt5/python 3.7 running in Spyder/iPython. When I run directly from the command line, it actually seems to work, so this could be an iPython or Spyder problem.
Upvotes: 2
Views: 3003
Reputation: 8029
The above code was only "broken" when using Spyder, and now works for newer versions of Spyder (versions 4.1+). So it wasn't a PyQt issue at all, ultimately, but an IDE interaction effect.
Upvotes: 1
Reputation: 183
Simply replacing self
with None
seems to display the QMessageBox in the middle of the screen.
QMessageBox.information(None, "Title", "Here is your informative message")
Upvotes: 2