GUIKIT PACKAGE TUTORIAL

GUI Life Cycles

Normally a live GUIObject instance will shut down and dispose of itself through the normal user action of closing a window or other interface widget that performs the equivalent user interface closure. Sometimes, however, it is convenient to programmatically close a live modeless interface. You can use CloseGUIObject on an active GUIObject instance to initiate a close action.

Note that CloseGUIObject only initiates a close request on the active interface and will not necessarily force the removal of all interface resources like ReleaseGUIObject if an initial GUIRun has requested the interface live on past its closing by using the option ReleaseMethod->Manual. If you want to ensure the original life cycle intent of a GUIRun call, it is recommended that you use CloseGUIObject instead of ReleaseGUIObject, since this will still close and possibly remove all resources associated with an interface if ReleaseMethod->Automatic was used or also allow the interface to live on if ReleaseMethod->Manual was used.

In[1]:=
Click for copyable input

Here you create a live user interface object by loading a calculator example.

In[2]:=
Click for copyable input
Out[2]=

You can verify this user interface is active even if not displayed using GUIObjectQ.

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

Instead of closing the active window with the mouse, you programmatically request the window close using CloseGUIObject.

In[4]:=
Click for copyable input

You now see that GUIObjectQ correctly reports this user interface, and its runtime representation as a GUIObject is no longer valid because GUIRun was initiated with the default Automatic release method.

In[5]:=
Click for copyable input
Out[5]=

Here you request instead that the definition should still stay active after it is closed using the option.

In[6]:=
Click for copyable input
Out[6]=

Instead of closing the active window with the mouse, you programmatically request the window close using CloseGUIObject.

In[7]:=
Click for copyable input

Here you see that GUIObjectQ correctly reports that this definition is still active because of the initial release method.

In[8]:=
Click for copyable input
Out[8]=

You can force complete termination of the interface using ReleaseGUIObject.

In[9]:=
Click for copyable input
In[10]:=
Click for copyable input
Out[10]=

If you decide that you want to dispose of an active modeless interface either before or after it is already displayed, you can use ReleaseGUIObject to force shutdown and remove all resources allocated to the running user interface.

Here you gain access to the GUIObject associated with an interface created in a modeless session.

In[11]:=
Click for copyable input
Out[11]=

Once you call ReleaseGUIObject, the interface is closed just as if the user closed the user interface through normal interaction.

In[12]:=
Click for copyable input

The resources and the associated with the user interface are now cleared.

In[13]:=
Click for copyable input
Out[13]=

You can also use the GUIObjectQ function to determine a live GUIObject reference from one that is now shut down.

In[14]:=
Click for copyable input
Out[14]=

However, it is probably more appropriate to use CloseGUIObject to only initiate the close action on the active interface. The reason for this is that GUIRun supports the option ReleaseMethod->Manual to allow you to reuse a live definition even after it is closed and reopened.

A common useful technique that takes advantage of the ReleaseMethod->Manual option of GUIRun is to define and use a single copy of a definition throughout a single kernel session, automatically handling the initial creation, as well as redisplaying it if it has happened to be closed manually by a user. The following wrapper function for requesting a specific interface definition supports all these concepts nicely, where represents the path to the definition you are interested in.

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