Input Dialogs

From the beginning pyFormex was intended as a framework for creating parametric models. This means that some parameter values need to be fed to the models to instantiate them. Often it is more appropriate to ask these values interactively from the user, rather than hardcoding them into a script.

The pyFormex user has full access to the Qt framework on which the GUI was built. Therefore he can built input dialogs as complex and powerful as he can imagine. However, directly dealing with the Qt libraries requires some skills and, for simple input widgets, more effort than needed.

Therefore pyFormex has a very powerful but still easy to use system for the creation of such dialogs. This document lays out the basic ideas of the dialog creation and use. The examples named in parentheses are available from the pyFormex menu Script->Scripts->userguide.

Modeless Dialogs

Modal Dialogs are a good solution when some information from the user is required to continue. But there are cases where we want to construct a dialog that stays open while allowing to further interact with the other windows. Such modeless dialogs need to be handled somewhat differently. The system can not wait for results. Instead you will have to define actions that are to be executed when activated, usually by clicking some button.

Here is the above dialog3 converted to a modeless dialog (dialog4):

"""Simple input dialog

A modeless dialog with the same input items as dialog3
"""
def print_array(nrows, ncols):
    A = np.arange(nrows*ncols).reshape(nrows,ncols)
    print(A)

def show():
    if dialog.validate():
        create_array(**dialog.results)

def close():
    dialog.close()

dialog = Dialog([
    _I('nrows', 3, min=0, text='Number of rows'),
    _I('ncols', 6, min=2, max=10, text='Number of columns'),
    ], actions=[('Close', close), ('Show', show)])
dialog.show()

The first function, create_array, is the same as before The next two functions are the actions that we want to be provided by the dialog: show creates and prints the array with the current input data; close just closes the dialog. The show function is important here: it first validates the current input data. If these are valid, they become available in a dict attribute results of the dialog, so we can use that directly to proceed with our purpose.

Finally, we construct the Dialog. Its first argument specifies the input items. This is exactly the same as the argument of askItems in script_dialog3. But we have a second keyword argument defining the actions to appear in the dialog. This is a list of tuples defining pushbuttons (actually it is the input for a ButtonBox constructor. Each button is defined by a tuple of a string and a function. The string is the text shown on the button. The function should take no arguments and will be executed when the button is pressed.

Now you can repeatedly change the input fields and click the Show button and see an array printed with the specified size. And when you’re done, click Close to close the window.

Dialog class

The Dialog class is the most important class in pyFormex for constructing user input dialogs. It is very versatile and dialogs can range from very simple to extremely comprehensive. In this user guide we will describe the most commonly used features. See the pyFormex reference manual for a full description.

Here is an example that is slightly more complex than the previous (dialog5):

"""Simple input dialog

A modeless dialog with the same input items as dialog3
"""
def create_grid(base, nrows, ncols, shear, color, custom):
    F = Formex(base).replic2(ncols, nrows).shear(0, 1, shear)
    if color == 'index':
        color = arange(nrows*ncols)
    elif color == 'custom':
        color = custom
    F.attrib(color=color)
    return F

def show():
    if dialog.validate():
        F = create_grid(**dialog.results)
        clear()
        draw(F)

def close():
    dialog.close()


flat()
dialog = Dialog([
    _I('base', choices=['4:0123', '3:016']),
    _I('nrows', 2, min=1, text='Number of rows'),
    _I('ncols', 3, min=1, text='Number of columns'),
    _I('shear', 0.0, text='Shear dx/dy'),
    _I('color', choices=['index', 'random', 'custom'], text='Color'),
    _I('custom', value='black', itemtype='color', text='Custom color'),
    ], actions=[('Close', close), ('Show', show)])
dialog.show()

for c in pf.GUI.children():
    print(c)

Input Items