«
class documentation

A collection of graphical elements and bindings used to display a complex object on a Tkinter Canvas. A canvas widget is responsible for managing the Canvas tags and callback bindings necessary to display and interact with the object. Canvas widgets are often organized into hierarchies, where parent canvas widgets control aspects of their child widgets.

Each canvas widget is bound to a single Canvas. This Canvas is specified as the first argument to the CanvasWidget's constructor.

Attributes. Each canvas widget can support a variety of "attributes", which control how the canvas widget is displayed. Some typical examples attributes are color, font, and radius. Each attribute has a default value. This default value can be overridden in the constructor, using keyword arguments of the form attribute=value:

>>> from nltk.draw.util import TextWidget
>>> cn = TextWidget(c, 'test', color='red')

Attribute values can also be changed after a canvas widget has been constructed, using the __setitem__ operator:

>>> cn['font'] = 'times'

The current value of an attribute value can be queried using the __getitem__ operator:

>>> cn['color']
red

For a list of the attributes supported by a type of canvas widget, see its class documentation.

Interaction. The attribute 'draggable' controls whether the user can drag a canvas widget around the canvas. By default, canvas widgets are not draggable.

CanvasWidget provides callback support for two types of user interaction: clicking and dragging. The method bind_click registers a callback function that is called whenever the canvas widget is clicked. The method bind_drag registers a callback function that is called after the canvas widget is dragged. If the user clicks or drags a canvas widget with no registered callback function, then the interaction event will propagate to its parent. For each canvas widget, only one callback function may be registered for an interaction event. Callback functions can be deregistered with the unbind_click and unbind_drag methods.

Subclassing. CanvasWidget is an abstract class. Subclasses are required to implement the following methods:

  • __init__: Builds a new canvas widget. It must perform the following three tasks (in order):

    • Create any new graphical elements.
    • Call _add_child_widget on each child widget.
    • Call the CanvasWidget constructor.
  • _tags: Returns a list of the canvas tags for all graphical elements managed by this canvas widget, not including graphical elements managed by its child widgets.

  • _manage: Arranges the child widgets of this canvas widget. This is typically only called when the canvas widget is created.

  • _update: Update this canvas widget in response to a change in a single child.

For a CanvasWidget with no child widgets, the default definitions for _manage and _update may be used.

If a subclass defines any attributes, then it should implement __getitem__ and __setitem__. If either of these methods is called with an unknown attribute, then they should propagate the request to CanvasWidget.

Most subclasses implement a number of additional methods that modify the CanvasWidget in some way. These methods must call parent.update(self) after making any changes to the canvas widget's graphical elements. The canvas widget must also call parent.update(self) after changing any attribute value that affects the shape or position of the canvas widget's graphical elements.

Method __getitem__ No summary
Method __init__ Create a new canvas widget. This constructor should only be called by subclass constructors; and it should be called only "after" the subclass has constructed all graphical canvas objects and registered all child widgets.
Method __repr__ No summary
Method __setitem__ Set the value of the attribute attr to value. See the class documentation for a list of attributes supported by this canvas widget.
Method bbox No summary
Method bind_click Register a new callback that will be called whenever this CanvasWidget is clicked on.
Method bind_drag Register a new callback that will be called after this CanvasWidget is dragged. This implicitly makes this CanvasWidget draggable.
Method canvas No summary
Method child_widgets No summary
Method destroy Remove this CanvasWidget from its Canvas. After a CanvasWidget has been destroyed, it should not be accessed.
Method height No summary
Method hidden No summary
Method hide Temporarily hide this canvas widget.
Method manage Arrange this canvas widget and all of its descendants.
Method move Move this canvas widget by a given distance. In particular, shift the canvas widget right by dx pixels, and down by dy pixels. Both dx and dy may be negative, resulting in leftward or upward movement.
Method moveto Move this canvas widget to the given location. In particular, shift the canvas widget such that the corner or side of the bounding box specified by anchor is at location (x, y).
Method parent No summary
Method show Show a hidden canvas widget.
Method tags No summary
Method unbind_click Remove a callback that was registered with bind_click.
Method unbind_drag Remove a callback that was registered with bind_drag.
Method update Update the graphical display of this canvas widget, and all of its ancestors, in response to a change in one of this canvas widget's children.
Method width No summary
Method __click If this CanvasWidget has a drag callback, then call it; otherwise, find the closest ancestor with a click callback, and call it. If no ancestors have a click callback, do nothing.
Method __drag If this CanvasWidget has a drag callback, then call it; otherwise, find the closest ancestor with a drag callback, and call it. If no ancestors have a drag callback, do nothing.
Method __motion_cb move this object to the new location
Method __press_cb record the button press event in self.__press
Method __release_cb unregister motion & button release callbacks.
Method __start_drag register a motion callback
Method _add_child_widget Register a hierarchical child widget. The child will be considered part of this canvas widget for purposes of user interaction. _add_child_widget has two direct effects:
Method _manage Arrange the child widgets of this canvas widget. This method is called when the canvas widget is initially created. It is also called if the user calls the manage method on this canvas widget or any of its ancestors.
Method _remove_child_widget Remove a hierarchical child widget. This child will no longer be considered part of this canvas widget for purposes of user interaction. _add_child_widget has two direct effects:
Method _tags No summary
Method _update Update this canvas widget in response to a change in one of its children.
Instance Variable __callbacks Registered callbacks. Currently, four keys are used: 1, 2, 3, and 'drag'. The values are callback functions. Each callback function takes a single argument, which is the CanvasWidget that triggered the callback.
Instance Variable __canvas This CanvasWidget's canvas.
Instance Variable __children This CanvasWidget's hierarchical child widgets.
Instance Variable __drag_x Where it's been moved to (to find dx)
Instance Variable __drag_y Where it's been moved to (to find dy)
Instance Variable __draggable Is this canvas widget draggable?
Instance Variable __hidden Undocumented
Instance Variable __parent This CanvasWidget's hierarchical parent widget.
Instance Variable __press The ButtonPress event that we're currently handling.
Instance Variable __updating Is this canvas widget currently performing an update? If it is, then it will ignore any new update requests from child widgets.
def __getitem__(self, attr): (source)
Returns
(any)the value of the attribute attr. See the class documentation for a list of attributes supported by this canvas widget.
def __init__(self, canvas, parent=None, **attribs): (source)

Create a new canvas widget. This constructor should only be called by subclass constructors; and it should be called only "after" the subclass has constructed all graphical canvas objects and registered all child widgets.

Parameters
canvas:Tkinter.CanvasThis canvas widget's canvas.
parent:CanvasWidgetThis canvas widget's hierarchical parent.
**attribsThe new canvas widget's attributes.
def __setitem__(self, attr, value): (source)

Set the value of the attribute attr to value. See the class documentation for a list of attributes supported by this canvas widget.

Returns
NoneUndocumented
def bbox(self): (source)
Returns
tuple(int, int, int, int)A bounding box for this CanvasWidget. The bounding box is a tuple of four coordinates, (xmin, ymin, xmax, ymax), for a rectangle which encloses all of the canvas widget's graphical elements. Bounding box coordinates are specified with respect to the coordinate space of the Canvas.
def bind_click(self, callback, button=1): (source)

Register a new callback that will be called whenever this CanvasWidget is clicked on.

Parameters
callback:functionThe callback function that will be called whenever this CanvasWidget is clicked. This function will be called with this CanvasWidget as its argument.
button:intWhich button the user should use to click on this CanvasWidget. Typically, this should be 1 (left button), 3 (right button), or 2 (middle button).
def bind_drag(self, callback): (source)

Register a new callback that will be called after this CanvasWidget is dragged. This implicitly makes this CanvasWidget draggable.

Parameters
callback:functionThe callback function that will be called whenever this CanvasWidget is clicked. This function will be called with this CanvasWidget as its argument.
def canvas(self): (source)
Returns
Tkinter.CanvasThe canvas that this canvas widget is bound to.
def child_widgets(self): (source)
Returns
list of CanvasWidgetA list of the hierarchical children of this canvas widget. These children are considered part of self for purposes of user interaction.
def destroy(self): (source)

Remove this CanvasWidget from its Canvas. After a CanvasWidget has been destroyed, it should not be accessed.

Note that you only need to destroy a top-level CanvasWidget; its child widgets will be destroyed automatically. If you destroy a non-top-level CanvasWidget, then the entire top-level widget will be destroyed.

Returns
NoneUndocumented
Raises
ValueErrorif this CanvasWidget has a parent.
def height(self): (source)
Returns
intThe height of this canvas widget's bounding box, in its Canvas's coordinate space.
def hidden(self): (source)
Returns
boolTrue if this canvas widget is hidden.
def hide(self): (source)

Temporarily hide this canvas widget.

Returns
NoneUndocumented
def manage(self): (source)

Arrange this canvas widget and all of its descendants.

Returns
NoneUndocumented
def move(self, dx, dy): (source)

Move this canvas widget by a given distance. In particular, shift the canvas widget right by dx pixels, and down by dy pixels. Both dx and dy may be negative, resulting in leftward or upward movement.

Parameters
dx:intThe number of pixels to move this canvas widget rightwards.
dy:intThe number of pixels to move this canvas widget downwards.
Returns
NoneUndocumented
def moveto(self, x, y, anchor='NW'): (source)

Move this canvas widget to the given location. In particular, shift the canvas widget such that the corner or side of the bounding box specified by anchor is at location (x, y).

Parameters
xUndocumented
yUndocumented
anchorThe corner or side of the canvas widget that should be moved to the specified location. 'N' specifies the top center; 'NE' specifies the top right corner; etc.
x,yThe location that the canvas widget should be moved to.
def parent(self): (source)
Returns
CanvasWidget or NoneThe hierarchical parent of this canvas widget. self is considered a subpart of its parent for purposes of user interaction.
def show(self): (source)

Show a hidden canvas widget.

Returns
NoneUndocumented
def tags(self): (source)
Returns
list of inta list of the canvas tags for all graphical elements managed by this canvas widget, including graphical elements managed by its child widgets.
def unbind_click(self, button=1): (source)

Remove a callback that was registered with bind_click.

Parameters
button:intWhich button the user should use to click on this CanvasWidget. Typically, this should be 1 (left button), 3 (right button), or 2 (middle button).
def unbind_drag(self): (source)

Remove a callback that was registered with bind_drag.

def update(self, child): (source)

Update the graphical display of this canvas widget, and all of its ancestors, in response to a change in one of this canvas widget's children.

Parameters
child:CanvasWidgetThe child widget that changed.
def width(self): (source)
Returns
intThe width of this canvas widget's bounding box, in its Canvas's coordinate space.
def __click(self, button): (source)

If this CanvasWidget has a drag callback, then call it; otherwise, find the closest ancestor with a click callback, and call it. If no ancestors have a click callback, do nothing.

def __drag(self): (source)

If this CanvasWidget has a drag callback, then call it; otherwise, find the closest ancestor with a drag callback, and call it. If no ancestors have a drag callback, do nothing.

def __motion_cb(self, event): (source)

Handle a motion event:
  • move this object to the new location
  • record the new drag coordinates

def __press_cb(self, event): (source)

Handle a button-press event:
  • record the button press event in self.__press
  • register a button-release callback.
  • if this CanvasWidget or any of its ancestors are draggable, then register the appropriate motion callback.

def __release_cb(self, event): (source)

Handle a release callback:
  • unregister motion & button release callbacks.
  • decide whether they clicked, dragged, or cancelled
  • call the appropriate handler.

def __start_drag(self, event): (source)

Begin dragging this object:
  • register a motion callback
  • record the drag coordinates

def _add_child_widget(self, child): (source)

Register a hierarchical child widget. The child will be considered part of this canvas widget for purposes of user interaction. _add_child_widget has two direct effects:

  • It sets child's parent to this canvas widget.
  • It adds child to the list of canvas widgets returned by the child_widgets member function.
Parameters
child:CanvasWidgetThe new child widget. child must not already have a parent.
def _manage(self): (source)

Arrange the child widgets of this canvas widget. This method is called when the canvas widget is initially created. It is also called if the user calls the manage method on this canvas widget or any of its ancestors.

Returns
NoneUndocumented
def _remove_child_widget(self, child): (source)

Remove a hierarchical child widget. This child will no longer be considered part of this canvas widget for purposes of user interaction. _add_child_widget has two direct effects:

  • It sets child's parent to None.
  • It removes child from the list of canvas widgets returned by the child_widgets member function.
Parameters
child:CanvasWidgetThe child widget to remove. child must be a child of this canvas widget.
@abstractmethod
def _tags(self): (source)
def _update(self, child): (source)

Update this canvas widget in response to a change in one of its children.

Parameters
child:CanvasWidgetThe child that changed.
Returns
NoneUndocumented
__callbacks: dictionary = (source)

Registered callbacks. Currently, four keys are used: 1, 2, 3, and 'drag'. The values are callback functions. Each callback function takes a single argument, which is the CanvasWidget that triggered the callback.

__canvas: Tkinter.Canvas = (source)

This CanvasWidget's canvas.

__children: list(CanvasWidget) = (source)

This CanvasWidget's hierarchical child widgets.

__drag_x: int = (source)

Where it's been moved to (to find dx)

__drag_y: int = (source)

Where it's been moved to (to find dy)

__draggable: bool = (source)

Is this canvas widget draggable?

__hidden: int = (source)

Undocumented

__parent: CanvasWidget or None = (source)

This CanvasWidget's hierarchical parent widget.

__press: event = (source)

The ButtonPress event that we're currently handling.

__updating: bool = (source)

Is this canvas widget currently performing an update? If it is, then it will ignore any new update requests from child widgets.