If you need to get some data you can use the ask_* commands from the API module muk3d.ui.forms. These are ok if you only want to get a single value, but tiresome if you need to get multiple values from the user. In that scenario, you'd use a different API function to build a dialog box that you can add multiple fields to.
1 2 3 4 5 6 7 8 9
from muk3d.ui.forms import ask, get_float, get_filename result = ask( fields=[get_float("Height", default_value=20, unit='length'), get_filename("Base grid", filters=['Grids (*.mgrid)',], select_existing_file=True) ], last_values_key='Remember_me', title='Get some stuff') print result['Height'] print result["Base grid"]
In Line 1 the relevant functions are imported. ask is the function that shows the dialog box, while get_float and get_filename are used to define the fields on the dialog box. To define these fields we pass a keyword argument called fields to the ask function. The fields argument should be a list that is populated with the return values of get_* functions.
In the example above, the first entry in the fields list is get_float. This will create a float field with the caption 'Height', a default value of 20, and the units are in m (this being a metric project). The second entry is get_file which is looking for a grid file, and the select_existing_file keyword argument set to True means that only files that exist can be selected.
The return value from the ask function is a Python dictionary with the field values in it. A dictionary is a data structure that stores values by a key. In this case, each value (user entered field value) is stored using a key that corresponds to each field's caption, as showing in Lines 8 and 9.
The following output is written to the output window.
A Dictionary in Python is a structure where values are accessed by their corresponding key. To access the value in the dictionary, you use the
[ ] operator on the dictionary. In this case, Lines 8 and 9 in the code above show how to get the values for the 'Height' and 'Base grid' fields.
If the user selects Cancel on the dialog box then an error message is written to the output window.
Error in script: test.py Traceback (most recent call last): TypeError: 'NoneType' object has no attribute '__getitem__'
This means that the return value from ask was None, and Line 8 is trying to access a value in it as if it was a valid dictionary. In order to fail gracefully when the user hits cancel, we need to check the return value and exit if its None.
The easiest way to ensure that the script only proceeds is to use the API function muk3d.util.is_valid. This function will check the return value of the ask function and exit if the result is None (i.e. the user pressed Cancel).
1 2 3 4 5 6 7 8 9 10 11
from muk3d.ui.forms import ask, get_float, get_filename from muk3d.util import is_valid result = ask( fields=[get_float("Height", default_value=20, unit='length'), get_filename("Base grid", filters=['Grids (*.mgrid)',], select_existing_file=True) ], last_values_key='Remember_me', title='Get some stuff') is_valid(result) print result['Height'] print result["Base grid"]
The is_valid function is imported in Line 2, and in Line 8 the result variable is checked. If its None (user pressed Cancel) then the script exits.
There are other fields that can be added to a dialog. These are:
- get_checkbox: Shows a checkbox for boolean choices.
- get_choice: Shows a list of values for the user to choose from.
- get_colour: User can pick a colour.
- get_integer: User can enter an integer value.
- get_text: User can enter any text.
- get_text_area: User can enter any multi-line text.