< All Topics

Getting a value from a user

If you want to get rid of hard wired variables in a script and instead allow the user to select values from a dialog  box, then the API muk3d.ui.forms provides some easy ways to build a dialog.

If you only have a single variable to get from the user, you can use one of the ask_* commands. 

from muk3d.ui.forms import ask_float

distance = ask_float("How far to the claster", "What is the distance to the claster?", unit='length')

print 'At a speed of 2 m/s it will take {} seconds to get there'.format(distance / 2.0)

In Line 1 the function ask_float is imported.  When called, a dialog box will pop up with a prompt for the user to enter a floating point (or decimal) number (Line 3) and the return value is assigned to the variable height.  When run, the above script shows the following dialog.

The output shown in the output window is below:

At a speed of 2 m/s it will take 250.0 seconds to get there

If you run the script again and click cancel, then we get an error message.

Error in script: test.py 
Traceback (most recent call last):
TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

If we hit cancel on the dialog box, the return value from the ask_float function is the Python value None. Trying to do division with None isn’t allowed in Python and so a TypeError is raised (because None is an invalid type for mathematical operations).  In order for our script to close gracefully in the event the user selects Cancel, we need to check that the return type is valid.

There are 2 ways we can do that.   The first is by doing a comparison to see if the return value is None and if so, exiting.

from muk3d.ui.forms import ask_float

distance = ask_float("How far to the claster", "What is the distance to the claster?", unit='length')

if distance is None:
    end()
    
print 'At a speed of 2 m/s it will take {} seconds to get there'.format(distance / 2.0)

In Line 4 the is operator is used to compare the value in distance to None.  If distance is None, end() is called and the script exits.

The function end() is not a standard Python keyword.   Its Muk3D specific. If you were writing a script to run in a regular Python interpreter, you would call sys.exit().  If you try and use end() in a regular script an exception will be raised.

Another way to validate a return value and exit is to use the function is_valid from the API muk3d.util.

from muk3d.ui.forms import ask_float
from muk3d.util import is_valid

distance = ask_float("How far to the claster", "What is the distance to the claster?", unit='length')

is_valid(distance)

# the script only gets to here if distance has a value, and is not None
 
print 'At a speed of 2 m/s it will take {} seconds to get there'.format(distance / 2.0)

In Line 2 is_valid is imported.  In Line 6, is_valid is used to check the value stored in distance. If its None, then the script exits.  If its not None, then the statements following are executed.

As well as the ask_float function, the following functions are also available:

  • ask_choice: Gets the value selected from a list of values.
  • ask_colour: Gets a colour
  • ask_filename: Gets a filename for reading or writing to.
  • ask_integer: Gets an integer.
  • ask_text: Gets any text.
Table of Contents