This example is a simple front end to the unix "cp" command that
demonstrates the use of the MrmNcreateCallback
to enter the
widgets created by UIL into module __main__
's dictionary. This
is somewhat cleaner than having to find the UIL-created widgets using
NameToWidget
. The files involved are panel_cp.uil,
panel_cp.uid and panel_cp.py.
panel_cp.uil contains the following:
module cp_front_end version = 'V1.0' names = case_sensitive procedure PyEval(string); object main : XmBulletinBoard { arguments { XmNwidth = 300; XmNheight = 400; }; controls { XmText src_file; XmText dst_file; XmPushButton button; }; }; object src_file : XmText { arguments { XmNy = 0; XmNx = 10; XmNwidth = 280; XmNheight = 40; XmNeditMode = XmSINGLE_LINE_EDIT; }; callbacks { MrmNcreateCallback = procedure PyEval("OnCreate('src_file_w')"); XmNvalueChangedCallback = procedure PyEval("OnValueChanged()"); }; }; object dst_file : XmText { arguments { XmNy = 60; XmNx = 10; XmNwidth = 280; XmNheight = 40; XmNeditMode = XmSINGLE_LINE_EDIT; }; callbacks { MrmNcreateCallback = procedure PyEval("OnCreate('dst_file_w')"); XmNvalueChangedCallback = procedure PyEval("OnValueChanged()"); }; }; object button : XmPushButton { arguments { XmNy = 110; XmNx = 10; XmNwidth = 100; XmNheight = 40; XmNlabelString = 'Do Copy'; XmNsensitive = false; }; callbacks { MrmNcreateCallback = procedure PyEval("OnCreate('button_w')"); XmNactivateCallback = procedure PyEval("DoCopy()"); }; }; end module;
panel_cp.uil is compiled to panel_cp.uid by the statement:
uil panel_cp.uil -o panel_cp.uid
panel_cp.py contains the following text:
#!/usr/local/bin/python import posix, os, sys, Mrm, Xm, Xt # ----------------------------------------- def OnCreate(name): # insert name=__widget__ into __main__'s dictionary sys.modules['__main__'].__dict__[name] = __widget__ # ----------------------------------------- def OnValueChanged(): if len(src_file_w.TextGetString()) > 0 and \ len(dst_file_w.TextGetString()) > 0: button_w.sensitive = 1 else: button_w.sensitive = 0 # ----------------------------------------- def DoCopy(): cmd = 'cp ' cmd = cmd + src_file_w.TextGetString() cmd = cmd + ' ' cmd = cmd + dst_file_w.TextGetString() print cmd posix.system(cmd) sys.exit() # ----------------------------------------- def main(): top_level = Xt.Initialize() mrm_hier = Mrm.OpenHierarchy("panel_cp.uid") main_w = mrm_hier.FetchWidget("main", top_level) main_w.ManageChild() top_level.RealizeWidget() Xt.MainLoop() main()
This example demonstrates using the OnCreate
MrmNcreateCallback
to access the __widget__
symbol and
insert an entry in the __main__
module's dictionary for the
XmText
and XmPushButton
widgets declared in the UIL.
This makes these widgets accessible as Python-wrapped widget objects
to the OnValueChanged
and OnCopy
callback routines,
without having to pass the widgets to these callbacks in the UIL tag
argument.
Another way to accomplish this is to insert the names into the
__main__
namespace explicitly in main()
, for example:
global src_file_w, dst_file_w ... main_w = mrm_hier.FetchWidget("main", top_level) src_file_w = main_w.NameToWidget('src_file') dst_file_w = main_w.NameToWidget('dst_file')
Here, 'src_file' and 'dst_file' are the widget's names as specified in
the UIL, and NameToWidget
is used to find the widgets, obtain
Python wrapped widget objects, and initialize src_file_w and
dst_file_w global variables.