7.3.3 Simple Button Using PyRegister

This example demonstrates the use of PyRegister() in UIL to register a Python function as an Mrm callback.

There are three files required--a .uil file named reg.uil, a .uid file named reg.uid, and a Python script named testreg.py.

reg.uil contains the following text:

    module BX
    version = 'V1.0'
    names = case_sensitive

    procedure MyPrint(widget);

    object main : XmBulletinBoard {
        arguments {
            XmNwidth = 200;
            XmNheight = 200;
        };
        controls {
            XmPushButton button;
        };
    };

    object button : XmPushButton {
        arguments {
            XmNx = 0;
            XmNy = 0;
            XmNwidth = 100;
            XmNheight = 40;
            XmNlabelString = 'PushMe';
        };
        callbacks {
            XmNactivateCallback = procedure MyPrint(main);
        };
    };

    end module;

reg.uid is created by the uil compiler as noted in the first example.

testreg.py (which must have execute permission) contains the following left-justified text:

    #!/usr/local/bin/python

    import os, sys, Mrm, Xt

    def MyPrint(widget, client, call):
        print 'MyPrint: widget =',widget,'client =',client

    def main():
        top_level = Xt.Initialize()

        mrm_hier = Mrm.OpenHierarchy(['reg.uid'])
        mrm_hier.PyRegister(MyPrint, 'widget')
        main_w = mrm_hier.FetchWidget('main', top_level)

        main_w.ManageChild()
        top_level.RealizeWidget()

        Xt.MainLoop()

    main()

The script is executed by the statement:

    testreg.py

When executed, the testreg.py script first declares the function MyPrint(). MyPrint is a typical Python callback function, and has widget, clientdata, and calldata arguments. MyPrint simply prints its widget and clientdata arguments.

Then the script declares the function main(). main opens the hierarchy defined by button.uid. main() then uses PyRegister() to register MyPrint as an Mrm callback function, with a client argument of type widget. Note that this must be done before the FetchWidget call, so that MyPrint() is registered before being used in the UIL. main() then fetches the widget named "main", which is a bulletin board, and its child, a pushbutton, manages these widgets and realizes the shell, and enters the Xt Event Loop.

Finally, the script invokes the main() function, which executes the code and builds the interface.

When the user pushes the pushbutton (labelled "Push Me" in the .uil file), the MyPrint() function is called. The widget argument is the pushbutton widget wrapped as a Python widget object; the client argument is the bulletin board widget main wrapped as a Python widget object; and the call argument is the call data structure appropriate to an XmNactivateCallback wrapped as a Python string object. MyPrint() prints the widget and call arguments.

Because MyPrint was declared to have a client data argument of type widget, when MyPrint is called the client argument is wrapped as a Python widget argument. The argument could also have been declared to be of type integer or string, in which case it would have be wrapped as the appropriate Python object. The declaration of the argument type of MyPrint() in the PyRegister function should match that declared in UIL.