Trouble using QInputDialog.getText

When I run the following code in the Python 3.8 IDLE no dialog appears and the print command does not execute:

from PyQt5.QtWidgets import *
title = “theTitle”
message = "Enter name of file "
answer = QInputDialog.getText(None, title, message)
print(answer)

The exact same lines of code work as expected in the QGIS Python pluggin. Why doesn’t this work in the Python 3.8 shell?

You’ve not mentioned any error message: I get QWidget: Must construct a QApplication before a QWidget when I run your code.

Thanks, Rob, for your reply. It led me to the answer. For some reason, when I run the code I get no error message, the script just terminates without comment. When I typed your error message into a Google search box it took me to “Error "Widget: Must construct a QApplication before a QWidget" - #2 by Mholscher” where in July 2021 Percy and Menno Holscher discuss this very same issue on this very same forum. After reading them, I added one line to my code (app = QApplication()) and now it works perfectly. (Note that my code already included " from PyQt5.QtWidgets import * " whereas Percy specifies . . . import QApplication among others.)

No worries. Good that you’ve sorted it.

It could very well be (in fact it’s almost certain) that our Python environments explain the reason that I got the error message, while you did not.

My setup:
OS: Linux Mint
Python: 3.6
IDE: Wing

Yes, apparently the environment matters: mine is
OS: Windows 10
Python: 3.8
IDE: IDLE 3.8.8
Again, thanks!