Package fife :: Package extensions :: Package pychan :: Package widgets' :: Module widget :: Class Widget
[hide private]
[frames] | no frames]

Class Widget

source code

object --+
         |
        Widget

This is the common widget base class, which provides most of the wrapping functionality.

Attributes

Widgets are manipulated (mostly) through attributes - and these can all be set by XML attributes. Derived widgets will have other attributes. Please see their New Attributes sections. The types of the attributes are pretty straightforward, but note that Position and Color attribute types will also accept fife.Point and fife.Color values.

Convenience Attributes

These attributes are convenience/shorthand versions of above mentioned attributes and assignment will reflect the associated attributes values. E.g. the following is equivalent:

  # Set X position, leave Y alone
  widget.x = 10
  # Same here
  posi = widget.position
  widget.position = (10, posi[1])

Here they are.

Instance Methods [hide private]
 
__init__(self, parent=None, name=None, size=None, min_size=None, max_size=None, helptext=None, position=None, style=None, hexpand=None, vexpand=None, font=None, base_color=None, background_color=None, foreground_color=None, selection_color=None, border_size=None, position_technique=None, is_focusable=None, comment=None)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
execute(self, bind)
Execute a dialog synchronously.
source code
 
requestFocus(self)
Requests focus.
source code
 
match(self, **kwargs)
Matches the widget against a list of key-value pairs.
source code
 
capture(self, callback, event_name="action", group_name="default")
Add a callback to be executed when the widget event occurs on this widget.
source code
 
isCaptured(self)
Check whether this widgets events are captured (a callback is installed) or not.
source code
 
show(self)
Show the widget and all contained widgets.
source code
 
hide(self)
Hide the widget and all contained widgets.
source code
 
isVisible(self)
Check whether the widget is currently shown, either directly or as part of a container widget.
source code
 
adaptLayout(self, recurse=True)
Execute the Layout engine.
source code
 
beforeShow(self)
This method is called just before the widget is shown.
source code
 
afterHide(self)
This method is called just before the widget is hidden.
source code
 
findChildren(self, **kwargs)
Find all contained child widgets by attribute values.
source code
 
getNamedChildren(self, include_unnamed=False)
Create a dictionary of child widgets with the keys being their name.
source code
 
findChild(self, **kwargs)
Find the first contained child widgets by attribute values.
source code
 
findChildByName(self, name)
Find first contained child widget by its name.
source code
 
addChild(self, widget)
This function adds a widget as child widget and is only implemented in container widgets.
source code
 
insertChild(self, widget, position)
This function inserts a widget a given index in the child list.
source code
 
insertChildBefore(self, widget, before)
Inserts a child widget before a given widget.
source code
 
addChildren(self, *widgets)
Add multiple widgets as children.
source code
 
removeChild(self, widget)
This function removes a direct child widget and is only implemented in container widgets.
source code
 
removeChildren(self, *widgets)
Remove a list of direct child widgets.
source code
 
removeAllChildren(self)
This function will remove all direct child widgets.
source code
 
mapEvents(self, eventMap, ignoreMissing=False)
Convenience function to map widget events to functions in a batch.
source code
 
setInitialData(self, data)
Set the initial data on a widget, what this means depends on the Widget.
source code
 
setData(self, data)
Set the user-mutable data on a widget, what this means depends on the Widget.
source code
 
getData(self)
Get the user-mutable data of a widget, what this means depends on the Widget.
source code
 
distributeInitialData(self, initialDataMap)
Distribute initial (not mutable by the user) data from a dictionary over the widgets in the hierachy using the keys as names and the values as the data (which is set via setInitialData).
source code
 
distributeData(self, dataMap)
Distribute data from a dictionary over the widgets in the hierachy using the keys as names and the values as the data (which is set via setData).
source code
 
collectDataAsDict(self, widgetNames)
Collect data from a widget hierachy by names into a dictionary.
source code
 
collectData(self, *widgetNames)
Collect data from a widget hierachy by names.
source code
 
listNamedWidgets(self)
This function will print a list of all currently named child-widgets to the standard output.
source code
 
stylize(self, style, **kwargs)
Recursively apply a style to all widgets.
source code
 
resizeToContent(self, recurse=True)
Try to shrink the widget, so that it fits closely around its content.
source code
 
expandContent(self, recurse=True)
Try to expand any spacer in the widget within the current size.
source code
 
_recursiveResizeToContent(self)
Recursively call resizeToContent.
source code
 
_recursiveExpandContent(self)
Recursively call expandContent.
source code
 
deepApply(self, visitorFunc, leaves_first=True)
Recursively apply a callable to all contained widgets and then the widget itself.
source code
 
getAbsolutePos(self)
Get absolute position on screen
source code
 
sizeChanged(self) source code
 
__str__(self)
str(x)
source code
 
__repr__(self)
repr(x)
source code
 
_setSize(self, size) source code
 
_getSize(self) source code
 
_setPosition(self, size) source code
 
_getPosition(self) source code
 
_setX(self, x) source code
 
_getX(self) source code
 
_setY(self, y) source code
 
_getY(self) source code
 
_setWidth(self, w) source code
 
_getWidth(self) source code
 
_setHeight(self, h) source code
 
_getHeight(self) source code
 
_getMinWidth(self) source code
 
_getMaxWidth(self) source code
 
_getMinHeight(self) source code
 
_getMaxHeight(self) source code
 
_setMinWidth(self, w) source code
 
_setMaxWidth(self, w) source code
 
_setMinHeight(self, h) source code
 
_setMaxHeight(self, h) source code
 
_setFont(self, font) source code
 
_getFont(self) source code
 
_getBorderSize(self) source code
 
_setBorderSize(self, size) source code
 
_getStyle(self) source code
 
_setStyle(self, style) source code
 
_getParent(self) source code
 
_setParent(self, parent) source code
 
_setName(self, name) source code
 
_getName(self) source code
 
_setFocusable(self, b) source code
 
_isFocusable(self) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Class Variables [hide private]
  ATTRIBUTES = [Attr('name'), PointAttr('position'), PointAttr('...
  DEFAULT_NAME = '__unnamed__'
  DEFAULT_HEXPAND = 0
  DEFAULT_VEXPAND = 0
  DEFAULT_MAX_SIZE = 500000, 500000
  DEFAULT_SIZE = -1,-1
  DEFAULT_MIN_SIZE = 0, 0
  DEFAULT_HELPTEXT = u""
  DEFAULT_POSITION = 0, 0
  DEFAULT_FONT = "default"
  DEFAULT_BORDER_SIZE = 0
  DEFAULT_POSITION_TECHNIQUE = "explicit"
  DEFAULT_COMMENT = u""
  HIDE_SHOW_ERROR = "...
  base_color = ColorProperty("BaseColor")
  background_color = ColorProperty("BackgroundColor")
  foreground_color = ColorProperty("ForegroundColor")
  selection_color = ColorProperty("SelectionColor")
  style = property(_getStyle, _setStyle)
  parent = property(_getParent, _setParent)
  name = property(_getName, _setName)
  x = property(_getX, _setX)
  y = property(_getY, _setY)
  width = property(_getWidth, _setWidth)
  height = property(_getHeight, _setHeight)
  min_width = property(_getMinWidth, _setMinWidth)
  min_height = property(_getMinHeight, _setMinHeight)
  max_width = property(_getMaxWidth, _setMaxWidth)
  max_height = property(_getMaxHeight, _setMaxHeight)
  size = property(_getSize, _setSize)
  position = property(_getPosition, _setPosition)
  font = property(_getFont, _setFont)
  border_size = property(_getBorderSize, _setBorderSize)
  is_focusable = property(_isFocusable, _setFocusable)
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, parent=None, name=None, size=None, min_size=None, max_size=None, helptext=None, position=None, style=None, hexpand=None, vexpand=None, font=None, base_color=None, background_color=None, foreground_color=None, selection_color=None, border_size=None, position_technique=None, is_focusable=None, comment=None)
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
(inherited documentation)

execute(self, bind)

source code 

Execute a dialog synchronously.

As argument a dictionary mapping widget names to return values is expected. Events from these widgets will cause this function to return with the associated return value.

This function will not return until such an event occurs. The widget will be shown before execution and hidden afterwards. You can only execute root widgets.

Note: This feature is not tested well, and the API will probably change. Otherwise have fun:

 # Okay this a very condensed example :-)
 return pychan.loadXML("contents/gui/dialog.xml").execute({ 'okButton' : True, 'closeButton' : False })

requestFocus(self)

source code 

Requests focus.

The widget must be focusable in order for this to work. See the is_focusable property.

match(self, **kwargs)

source code 

Matches the widget against a list of key-value pairs. Only if all keys are attributes and their value is the same it returns True.

capture(self, callback, event_name="action", group_name="default")

source code 

Add a callback to be executed when the widget event occurs on this widget.

The callback must be either a callable or None. The old event handler (if any) will be overridden by the callback. If None is given, the event will be disabled. You can query isCaptured wether this widgets events are currently captured.

It might be useful to check out tools.callbackWithArguments.

Parameters:
  • callback - Event callback - may accept keyword arguments event and widget.

adaptLayout(self, recurse=True)

source code 

Execute the Layout engine. Automatically called by show. In case you want to relayout a visible widget. This function will automatically perform the layout adaption from the top-most layouted widget.

To make this clear consider this arrangement:

     VBox 1
     - Container
       - VBox 2
         - HBox
           - Label

If you call adaptLayout on the Label the layout from the VBox 2 will get recalculated, while the VBox 1 stays untouched.

@param recurse Pass False here to force the layout to start from this widget.

beforeShow(self)

source code 

This method is called just before the widget is shown. You can override this in derived widgets to add finalization behaviour.

NOTE:

  • if your widget is a container, you have to call _resetTiling(), as you will loose this call by using your override method

afterHide(self)

source code 

This method is called just before the widget is hidden. You can override this in derived widgets to add finalization behaviour.

findChildren(self, **kwargs)

source code 

Find all contained child widgets by attribute values.

Usage:

 closeButtons = root_widget.findChildren(name='close')
 buttons = root_widget.findChildren(__class__=pychan.widgets.Button)

getNamedChildren(self, include_unnamed=False)

source code 

Create a dictionary of child widgets with the keys being their name. This will contain only Widgets which have a name different from "__unnamed__" (which is the default).

@param include_unnamed Defaults to false. If this is true unnamed widgets are added, too.

The values are lists of widgets, so not only unique names are handled correctly.

Usage:

       children = widget.getNamedChildren()
       for widget in children.get("info",[])
               print widget.name , " == info"

findChild(self, **kwargs)

source code 

Find the first contained child widgets by attribute values.

Usage:

 closeButton = root_widget.findChild(name='close')

findChildByName(self, name)

source code 

Find first contained child widget by its name.

Note that this is the fast version of findChild(name="...") and that you don't have to call this explicitly, it is used if possible.

addChild(self, widget)

source code 

This function adds a widget as child widget and is only implemented in container widgets.

You'll need to call adaptLayout if the container is already shown, to adapt the layout to the new widget. This doesn't happen automatically.

insertChild(self, widget, position)

source code 

This function inserts a widget a given index in the child list.

See addChild and insertChildBefore

insertChildBefore(self, widget, before)

source code 

Inserts a child widget before a given widget. If the widget isn't found, the widget is appended to the children list.

See addChild and insertChild

addChildren(self, *widgets)

source code 

Add multiple widgets as children. Only implemented for container widgets. See also addChild

Usage:

       container.addChildren( widget1, widget2, ... )
       # or you can use this on a list
       container.addChildren( [widget1,widget2,...] )

removeChild(self, widget)

source code 

This function removes a direct child widget and is only implemented in container widgets.

You'll need to call adaptLayout if the container is already shown, to adapt the layout to the removed widget. This doesn't happen automatically.

removeChildren(self, *widgets)

source code 

Remove a list of direct child widgets. All widgets have to be direct child widgets. To 'clear' a container take a look at removeAllChildren. See also removeChild.

Usage:

       container.removeChildren( widget1, widget2, ... )
       # or you can use this on a list
       container.removeChildren( [widget1,widget2,...] )

removeAllChildren(self)

source code 

This function will remove all direct child widgets. This will work even for non-container widgets.

mapEvents(self, eventMap, ignoreMissing=False)

source code 

Convenience function to map widget events to functions in a batch.

Subsequent calls of mapEvents will merge events with different widget names and override the previously set callback. You can also pass None instead of a callback, which will disable the event completely.

Parameters:
  • eventMap - A dictionary with widget/event names as keys and callbacks as values.
  • ignoreMissing - Normally this method raises an RuntimeError, when a widget can not be found - this behaviour can be overriden by passing True here.

    The keys in the dictionary are parsed as "widgetName/eventName" with the slash separating the two. If no slash is found the eventName is assumed to be "action".

    Additionally you can supply a group name or channel "widgetName/eventName/groupName". Event handlers from one group are not overridden by handlers from another group. The default group name is "default".

    Example:

           guiElement.mapEvents({
                   "button" : guiElement.hide,
                   "button/mouseEntered" : toggleButtonColorGreen,
                   "button/mouseExited" :  toggleButtonColorBlue,
           })
    

setInitialData(self, data)

source code 

Set the initial data on a widget, what this means depends on the Widget. In case the widget does not accept initial data, a RuntimeError is thrown.

setData(self, data)

source code 

Set the user-mutable data on a widget, what this means depends on the Widget. In case the widget does not accept data, a RuntimeError is thrown. This is inverse to getData.

getData(self)

source code 

Get the user-mutable data of a widget, what this means depends on the Widget. In case the widget does not have user mutable data, a RuntimeError is thrown. This is inverse to setData.

distributeInitialData(self, initialDataMap)

source code 

Distribute initial (not mutable by the user) data from a dictionary over the widgets in the hierachy using the keys as names and the values as the data (which is set via setInitialData). If more than one widget matches - the data is set on ALL matching widgets. By default a missing widget is just ignored.

Use it like this:

 guiElement.distributeInitialData({
      'myTextField' : 'Hello World!',
      'myListBox' : ["1","2","3"]
 })

distributeData(self, dataMap)

source code 

Distribute data from a dictionary over the widgets in the hierachy using the keys as names and the values as the data (which is set via setData). This will only accept unique matches.

Use it like this:

 guiElement.distributeData({
      'myTextField' : 'Hello World!',
      'myListBox' : ["1","2","3"]
 })

collectDataAsDict(self, widgetNames)

source code 

Collect data from a widget hierachy by names into a dictionary. This can only handle UNIQUE widget names (in the hierachy) and will raise a RuntimeError if the number of matching widgets is not equal to one.

Usage:

 data = guiElement.collectDataAsDict(['myTextField','myListBox'])
 print "You entered:",data['myTextField']," and selected ",data['myListBox']

collectData(self, *widgetNames)

source code 

Collect data from a widget hierachy by names. This can only handle UNIQUE widget names (in the hierachy) and will raise a RuntimeError if the number of matching widgets is not equal to one.

This function takes an arbitrary number of widget names and returns a list of the collected data in the same order.

In case only one argument is given, it will return just the data, with out putting it into a list.

Usage:

 # Multiple element extraction:
 text, selected = guiElement.collectData('myTextField','myListBox')
 print "You entered:",text," and selected item nr",selected
 # Single elements are handled gracefully, too:
 test = guiElement.collectData('testElement')

listNamedWidgets(self)

source code 

This function will print a list of all currently named child-widgets to the standard output. This is useful for debugging purposes.

resizeToContent(self, recurse=True)

source code 

Try to shrink the widget, so that it fits closely around its content. Do not call directly.

expandContent(self, recurse=True)

source code 

Try to expand any spacer in the widget within the current size. Do not call directly.

_recursiveResizeToContent(self)

source code 

Recursively call resizeToContent. Uses deepApply. Do not call directly.

_recursiveExpandContent(self)

source code 

Recursively call expandContent. Uses deepApply. Do not call directly.

__str__(self)
(Informal representation operator)

source code 

str(x)

Overrides: object.__str__
(inherited documentation)

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)

Class Variable Details [hide private]

ATTRIBUTES

Value:
[Attr('name'), PointAttr('position'), PointAttr('min_size'), PointAttr\
('size'), PointAttr('max_size'), ColorAttr('base_color'), ColorAttr('b\
ackground_color'), ColorAttr('foreground_color'), ColorAttr('selection\
_color'), Attr('style'), Attr('font'), IntAttr('border_size'), Attr('p\
osition_technique'), IntAttr('vexpand'), IntAttr('hexpand'), UnicodeAt\
tr('helptext'), BoolAttr('is_focusable'), UnicodeAttr('comment')]

HIDE_SHOW_ERROR

Value:
"""\
		You can only show/hide the top widget of a hierachy.
		Use 'addChild' or 'removeChild' to add/remove labels for example.
		"""