Input option from command line

Hi,

I am working on a script and I need to ask the user some parameters. I am using the function input:

def func():
     option1 = input("Bla bla")
     option2 = input("Bla bla")
     option3 = input("Bla bla")

The first input is working but the following ones don’t. What I am doing wrong?
Same code works outside the function.

1 Like

You can use a nice GUI widget to get additional inputs.

For a single input you can do this:

result = qt.QInputDialog().getText(None, "Some Title","Some description")

image

If you need multiple inputs then you can put together a dialogbox with a few fields in 10-15 lines of Python code; or create a dialog in Qt Designer (menu: Edit / Application settings / Developer / Qt Designer → launch):

You can display the dialog and get the entered values like this:

widget = slicer.util.loadUI(r'c:\D\S4\Base\QTGUI\Resources\UI\qSlicerSettingsUserInformationPanel.ui')
widget.show()
ui = slicer.util.childWidgetVariables(widget)
print(ui.NameLineEdit.text)

This widget does not have an OK/Cancel button but you can add them in Qt Designer.

3 Likes

We could also look into overriding the input and raw_input built-in function to display a dialog when running without --testing flag.

That said, to be robust, modules should provide default values and nicely handle the case of being executed as self-test.

1 Like