GUIKIT PACKAGE TUTORIAL

Creating Reusable Definitions

The GUIKit framework makes it relatively easy to reuse interface definitions because of its automatic use of private symbol contexts for each instance of a definition. There are, however, a number of points to keep in mind when writing Mathematica script code for user interfaces.

  • Use Needs["Package`"] instead of Get["Package`"] to avoid unnecessary package reloading if multiple instances of an interface are possible.
  • Avoid explicit use of contexted symbols such as , since this will be the same global variable for all instances of the interface. This may, however, be a desired way of creating a global state shared between instances if that is what you intend.
    • Consider designing your definition with a root widget of and not . will automatically wrap a panel in a window when it is displayed, and by not forcing a specific frame instance in your definition, another user can load your definition into their own window.

    Here is an example that shows how one definition can be easily used in another window with multiple instances, all separate in state but accessible in the new window.

    In[1]:=
    Click for copyable input

    As it is defined, the Counter example is a fully functional stand-alone compound widget comprising three individual widgets.

    In[2]:=
    Click for copyable input

    Here is a screen shot of what a single Counter widget renders.

    The Counter definition is also written so that multiple instances can be reused in the same window, and by using the ExposeWidgetReferences option of Widget[], you can make sure each instance is registered with unique names within the new parent window.

    In[3]:=
    Click for copyable input
    In[4]:=
    Click for copyable input
    Out[4]=

    Here is a screen shot of what the multiple Counter widgets in a single panel render.

    In[5]:=
    Click for copyable input
    Out[5]=
    In[6]:=
    Click for copyable input
    Out[6]=
New to Mathematica? Find your learning path »
Have a question? Ask support »