5

I need the user to input a value. I'm using input() on qgis python console, but getting this error:

RuntimeError: input(): lost sys.stdin

How can I solve it?

2
  • 3
    I don't think you can use input() in a pyqgis script unless you are executing from a CMD window - the python window isn't stdin or stdout. You would need to ask your question in a form of some sort. Commented Nov 28, 2019 at 3:24
  • stackoverflow.com/q/35707849/1863709 This answer may help you, but it ist qt4, not qt5. Commented Nov 28, 2019 at 7:09

1 Answer 1

5
from PyQt5.QtWidgets import QInputDialog
def getTextInput(title, message):
    answer = QInputDialog.getText(None, title, message)
    if answer[1]:
        print(answer[0])
        return answer[0]
    else:
        return None

To ask for a user input in QGIS-Console, you can use a QDialog object. There are some kinds of "ready to use" dialogs you could choose from. One of them is QInputDialog which itself can be shown with different types of widgets to get users choice. Above I used the Method getText() which opens a dialog with a single line input. Please read the doc to get more informations about that.

You also can call QInputDialog directly in the Python console:

answer = QInputDialog().getText(None, "Input", "Your input:")

answer is always a tuple like (text, ok) to examine, if input was cancelled and what input was made.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.