Package autodesk_toxik :: Module ui
[frames] | no frames]

Module ui

User Interface module.

This module provides convenience functions and a {Dialog} class to create dialogs that have the Toxik look and feel.

This module also provides access to the current workspace of the Toxik application when imported in the embedded Python interpreter.

UI Convenience Functions

The functions in this series create a top level window and a root container (if needed), then create a dialog box and bring it up.

When the dialog is dismissed by the user, the function returns a dictionary of values for all significant widgets. The key is the name of the widget and the value represents the state or data stored by the widget.

In addition to the widgets explicitely created by the caller, there is a key "CloseButtonClicked". The value of this key is the name of the push button that was clicked to close the dialog.

Note: If one wishes to create and launch a Dialog with more control over the widgets in the dialog, such as adding callback functions, please refer to the section on the Dialog class.

Widgets for which data is returned are:

Classes
  Cursor
  BrowserScheme
This class holds a set of string constants that define the various browser schemes present in Toxik, in other words, the types of entity that can be browsed.
  CursorGuard
Cursor guard to change the current cursor to an alternate cursor, automatically reverting to previous cursor when going out of scope.
  Dialog
This class provides fine control over the widgets within the a dialog returned by the createDialog() function call.
  EventType
  UiError
  Workspace
The workspace provides access to the context of the interactive application and thus only makes sense when scripts are executed in the embedded interpreter.
Functions
 
showOkCancelMessageBox(title, msg, cancelButton=True)
Pops-up a message box with an OK button and optionally a Cancel button.
Dialog
createDialog(...)
Create and return an instance of a Dialog object.
str
makeXmlFromWidgetList(...)
Generates an xml string to create a simple layout of widgets.
str
showBrowser(...)
Shows a Toxik file system browser.
dict
showDialog(...)
This function receives a menu description in xml notation contained in the given file.
str
showMessageBox(...)
Shows a message box dialog with the specified close buttons and returns the label of the button that was clicked to close the dialog.
dict
showSimpleDialog(...)
This function creates a dialog for the widgets requested, with automatic widget layout.
Variables
  __package__ = 'autodesk_toxik'
Function Details

showOkCancelMessageBox(title, msg, cancelButton=True)

 

Pops-up a message box with an OK button and optionally a Cancel button. If the Cancel button is clicked, sys.exit(0) is invoked and the current script execution terminates.

Parameters:
  • title (str) - Message box title.
  • msg (str or list of str) - Message multi-line text which can either be a list of strings or a single string contaning newline characters.

createDialog(...)

 

Create and return an instance of a Dialog object. This function uses the same parameters as showDialog(), except that instead of passing an xml file, we pass it an xmlString. Using the Dialog object returned, a user can use methods of the Dialog class to have finer control over the Dialog.

Note that this is a special function in this series in that it only returns the instance of Dialog object created but not actually launching the dialog. However, this is also the only function a user can use to obtain the actual Dialog object.

Parameters:
  • title (str)
  • xmlString (str)
  • width (int) - Dialog width (Defaults to -1).
  • height (int) - Dialog height (Defaults to -1).
  • initialValues (dict) - Initial widget values (Defaults to {}).
Returns: Dialog
The created Dialog instance.

makeXmlFromWidgetList(...)

 

Generates an xml string to create a simple layout of widgets. The layout is the same as created by the function showSimpleDialog(). Please refer to it for a description.

Label widgets created by this utility are named "Label_<label>", where <label> is the text for the label. This is important to consider when some actions, like hide/show and enable/disable are to be performed on labels.

In the example below, the textfield widget is named 'PhoneField' and the auto-generated widget for the label is named 'Label_Phone'. To respectively hide the textfield and the label, one would call: dialog.hide( 'PhoneField' ) and dialog.hide( 'Label_Phone' ).

Example:

   widgets = [ ('TextField', 'PhoneField', 'Phone', '1-800-CallMe') ]
   xml = ui.makeXmlFromWidgetList( widgets )
   dialog = ui.createDialog( 'MyDialog', xml, 75, 50 )
   dialog.launch()
Parameters:
  • widgets (list of str) - List of widgets.
  • closeButtons (list of str) - List of close buttons. Defaults to [].
Returns: str
The generated XML string.

showBrowser(...)

 

Shows a Toxik file system browser.

The browser has the given title, and is initially in the directory indicated by initialPath (if not specified, it's located at the root of the file system).

Parameters:
  • title (str) - The title of the browser.
  • initialPath (str) - The browser's initial path. Defaults to "".
  • pathValidationCb (callable) - Path validation callback. Defaults to None. This callback is invoked when the user hits the OK button, and is given two parameters: the path that's in focus in the browser and the path of the current directory. The callback must return either the path to be returned by the showBrowser() call, which closes the browser, or an empty string to refuse the browsed path and keep the browser opened. By default, when no path validation callback is specified, the path in focus is returned if any, otherwise the path of the current directory is returned.
Returns: str
The showBrowser() call returns when the user hits the OK or Cancel button, with the path returned by pathValidationCb or an empty string, respectively.

Note: Currently showBrowser() only works when the script is run within Toxik, i.e. in its embedded python interpreter.

showDialog(...)

 

This function receives a menu description in xml notation contained in the given file. The description must define at least one close button, named CloseButton_<name>. Close buttons cause the dialog to terminate.

The name of the widgets in the dictionary corresponds to the name defined in the xml description (per the <Widget Name = "SomeName"> entry). That does not include the close buttons. For convenience, a virtual close button named "CloseButtonClicked" is put in the dictionary: the value is the widget name of the button that caused the dialog to close.

Note that for the dialog, close buttons have no other semantic than being used to close the dialog. The semantic is up to the caller as the values stored in the widgets are not affected.

Parameters:
  • title (str)
  • xmlFile (str)
  • width (int) - Dialog width (Defaults to -1).
  • height (int) - Dialog height (Defaults to -1).
  • initialValues (dict) - Initial widget values (Defaults to {}).
Returns: dict
A dict with widget names as keys and their final state as values.

showMessageBox(...)

 

Shows a message box dialog with the specified close buttons and returns the label of the button that was clicked to close the dialog.

Example:

   closeButton = ui.showMessageBox(
       "Warning",
       ["You are about to erase this file:",
       filename,
       "This operation cannot be undone."],
       ["OK", "Cancel"])
   if closeButton == "OK":
       erase(filename)
Parameters:
  • title (str)
  • messages (list of str) - List of messages to be displayed in the message box.
  • closeButtons (list of str) - List of close buttons. Defaults to [].
Returns: str
A string representing the label of the close button that was activated.

showSimpleDialog(...)

 

This function creates a dialog for the widgets requested, with automatic widget layout. The layout is very simple. For each entry in the provided list, a pair of label and widget is created. Each pair is drawn vertically, in the order they appear in the list. By default, one close button is provided, labeled "Ok". Optionally, a second close button can be provided, labeled "Cancel". Note that regardless of which close button was activated, the values in the dictionary are those current at the time the dialog was closed: it is up to the caller to ignore the values if the Cancel button was used.:

   +---------+------------------+
   | Label1  | Widget1          |
   +---------+------------------+
   | Label2  | Widget           |
   +----+----+---------+--------+
   | Ok |              | Cancel |
   +----+--------------+--------+

This function receives a list of 4-tuples:

  • widget type
  • widget name
  • label text (often same as name)
  • initial state

Type: 3 types of widgets are available: TextField, ToggleButton and Menu. Name: a string used to identify the widget. Must be unique. Label text: of the label that shows on the left of the widget. Initial state depends on the widget type:

  • TextField: a string or an empty string
  • ToggleButton: True or False (i.e. on and off) - Menu: a tuple consisting of list of string, items that will populate the menu) and an index that sets which item is selected by default. Index of first item is 0.

Example:

   widgets = [ ( 'TextField', 'Show', 'Show', 'Academy Awards' ),
               ( 'ToggleButton', 'Win', 'Do you win ?', True ),
               ( 'Menu', 'Category', 'Category', (['Editing', 'Directing', 'Music'], 1) ) ]

   result = ui.showSimpleDialog( 'MyDialog', widgets, 400, True )
Parameters:
  • title (str)
  • list (list) - list of 4-tuple.
  • width (int)
  • cancelButton (bool) - Indicates whether or not a cancel button should be included. Default is False.
  • initialValues (dict) - Initial widget values.
Returns: dict
A dict with widget names as keys and their final state as values.