7.3.1 Simple Button using AddCallback

This example demonstrates about the simplest possible example of using the Mrm module. There are three files required--a .uil file named button.uil, a .uid file named button.uid, and a Python script named testbutton.py.

button.uil contains the following text:

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

    object main : XmBulletinBoard {     ! bulletin board parent
        arguments {
            XmNwidth = 200;
            XmNheight = 200;
        };
        controls {
            XmPushButton button;        ! main is parent of button
        };
    };

    object button : XmPushButton {      ! button is labelled Push Me
        arguments {
            XmNx = 0;
            XmNy = 0;
            XmNwidth = 100;
            XmNheight = 40;
            XmNlabelString = 'Push Me';
        };
    };

    end module;

button.uil is compiled to button.uid by the statement:

    uil button.uil -o button.uid

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

    #!/usr/local/bin/python

    import os, sys, Mrm, Xt

    def OnActivate(widget, client, call):
        print 'OnActivate:',widget,client

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

        # fetch the hierarchy
        mrm_hier = Mrm.OpenHierarchy('button.uid')
        main_w = mrm_hier.FetchWidget('main', top_level)
        button_w = main_w.NameToWidget('button')
        button_w.AddCallback('activateCallback', OnActivate, 'Hi')

        # manage widgets, realize shell
        main_w.ManageChild()
        top_level.RealizeWidget()

        Xt.MainLoop()

    main()

The script is executed by the statement:

    testbutton.py

When executed, the testbutton.py script first declares the function OnActivate(). OnActivate is a typical callback function, called with widget, client data and call data arguments. It prints its widget and client data argument. The call data argument is binary, hence ugly when printed.

Then the script declares the function main(). main() opens the hierarchy defined by button.uid, fetches the widget named "main", which is a bulletin board, and its child, a pushbutton. Next, main() uses NameToWidget to obtain a Python widget object for the button widget, and adds OnActivate() as the XmNactivateCallback function, passing "Hi" as the client data. main() then manages these widgets, realizes the shell, and enters the Xt Event Loop.

Finally, the script invokes main(), which executes the function main(), and generates the interface as described above.

When the user pushes the pushbutton (labelled "Push Me" by the XmNlabelString resource specified in the .uil file), the OnActivate() function is called with the string "Hi" as its client data. OnActivate prints "Hi" on stdout.