GUIKIT 程序包 教程

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 Wolfram Language 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 MyPackage`MySymbol, 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 "Panel" and not "Frame". GUIKit` 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.

    Click for copyable input

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

    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.

    Click for copyable input
    Click for copyable input

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

    Click for copyable input
    Click for copyable input