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.

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 continue working with the other windows. Such modeless dialogs need to be handled somewhat different. The system can not wait for results. Instead you will need to define actions that need to be executed when activated.

Here is a simple modeless dialog, containing the same input items as dialog3 above (dialog4):

def create_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 function we actually want to accomplish: print an (nrows, ncols) array. The next two functions are the actions that we want to be provided by the dialog. The show function validates the current input data, and if they’re valid, calls create_array to do the work.

Let’s disect this, starting with the creation of the dialog near the bottom. The Dialog class class initializer requires a list of input items as first argument, just like the askItems function for creating Modal Dialogs. But it has many optional arguments. The actions options defines the buttons that will be provided on the dialog to initiate actions. 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. In our case we defined two buttons: Show, to create and print the array according to the user input; and Close, to close the dialog when we’re done.

Dialog class

Input Items