GUIKIT 程序包 教程

GUIKit Example: Making Progress (Extended)

Click for copyable input


This example demonstrates various techniques for designing a progress bar dialog and a number of options available to wrap the reusable widget involved.

Basic Implementation

Here is one technique for defining a progress bar using the Wolfram Language expression syntax.

Click for copyable input

A progress bar would be most useful in a modeless session where it could be used to update the state of another ongoing kernel evaluation.

Click for copyable input

There are three important widgets in the dialog that have been registered with reference names for easy lookup. Here are the most common properties worth setting during the lifetime of the dialog.

Click for copyable input
Click for copyable input
Click for copyable input

Once the dialog is not needed, you can call ReleaseGUIObject to dispose of the instance.

Click for copyable input

The following variants also show different techniques for defining a reusable progress dialog within the layout of an AddOn, and discuss the many options available for exposing only a subset of the properties and widgets that make up the user interface.

Example 1 of a Progress Bar Implementation

Here is an example that provides a progress bar implementation similar to the J/Link example.

In this version, you postmanipulate the Java objects in the Wolfram Language by looking up the instances from their names after loading the definition file. After modifying the frame's title and the label's text, you will then call GUIRun, which displays the dialog.

Click for copyable input

Here is a function that simulates a typical interaction with the GUIObject representing a progress bar dialog.

Click for copyable input

To use the progress bar in a computation, you call CreateProgressBar1, which loads the user interface definition and returns a GUIObject. Then, while the computation is running, you periodically change the value property to update the bar's appearance. When the computation is done, you call ReleaseGUIObject.

Click for copyable input
Click for copyable input

You can also expose certain properties as optional arguments to the CreateProgressBar1 function to reuse the dialog in multiple situations.

Click for copyable input
Click for copyable input
Example 2A of a Progress Bar Implementation

This example illustrates how you can alternatively create reusable versions of a progress bar or any widget by placeholding dynamic properties that may need to change for each instance. This is done by using WidgetReference["#n"] within a base .m and creating various wrapper .m files that define these needed arguments to the base script externally.

In this example, you use a base user interface called ProgressBar2Base.m that contains uses of WidgetReference["#n"] and expects these values to be passed as initial arguments when called.

The source of ProgressBar2Base.m clarifies what dynamic elements of the progress bar are unique to this instance by only exposing certain properties within ProgressBar2Base.m, but setting all these modifiable properties within the wrapping .m file.

Click for copyable input

Here is the revised CreateProgressBar2A implementation reusing the same RunProgressBar.

Click for copyable input
Click for copyable input
Example 2B of a Progress Bar Implementation

Taking this wrapper and base .m a step further, the Wolfram Language functions that load and run GUIObject instances can also pass script arguments when they are called using an optional second argument. Here you show how the Wolfram Language code can dynamically pass arguments to the base .m file that you used previously, duplicating the functionality of example 1, but greatly simplifying the interaction with the reusable user interface by not seeing all the individual property processing calls within the CreateProgressBar1 Wolfram Language code.

Notice how the second argument to GUILoad is simply {title,caption,percent}, which will be registered in the widget name registry as "#1", "#2", and "#3", respectively.

The two-argument call to GUILoad using ProgressBar2Base.m is the equivalent of example 2A of wrapping ProgressBar2Base.m with ProgressBar2.m containing the values desired.

Click for copyable input

Here is the revised CreateProgressBar2B implementation reusing the same RunProgressBar.

Click for copyable input
Click for copyable input