class CanvasWidget: (source)
Known subclasses: nltk.draw.tree.TreeSegmentWidget
, nltk.draw.tree.TreeWidget
, nltk.draw.util.AbstractContainerWidget
, nltk.draw.util.ScrollWatcherWidget
, nltk.draw.util.SequenceWidget
, nltk.draw.util.SpaceWidget
, nltk.draw.util.StackWidget
, nltk.draw.util.TextWidget
Constructor: CanvasWidget(canvas, parent, **attribs)
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 |
Register a new callback that will be called whenever this CanvasWidget is clicked on. |
Method | bind |
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 |
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 |
Remove a callback that was registered with bind_click. |
Method | unbind |
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 |
move this object to the new location |
Method | __press |
record the button press event in self.__press |
Method | __release |
unregister motion & button release callbacks. |
Method | __start |
register a motion callback |
Method | _add |
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 |
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 |
Where it's been moved to (to find dx) |
Instance Variable | __drag |
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. |
nltk.draw.tree.TreeSegmentWidget
, nltk.draw.tree.TreeWidget
, nltk.draw.util.BoxWidget
, nltk.draw.util.BracketWidget
, nltk.draw.util.OvalWidget
, nltk.draw.util.ParenWidget
, nltk.draw.util.SequenceWidget
, nltk.draw.util.StackWidget
, nltk.draw.util.TextWidget
Returns | |
(any) | the value of the attribute attr. See the class documentation for a list of attributes supported by this canvas widget. |
nltk.draw.tree.TreeSegmentWidget
, nltk.draw.tree.TreeWidget
, nltk.draw.util.AbstractContainerWidget
, nltk.draw.util.ScrollWatcherWidget
, nltk.draw.util.SequenceWidget
, nltk.draw.util.SpaceWidget
, nltk.draw.util.StackWidget
, nltk.draw.util.TextWidget
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.Canvas | This canvas widget's canvas. |
parent:CanvasWidget | This canvas widget's hierarchical parent. |
**attribs | The new canvas widget's attributes. |
nltk.draw.tree.TreeSegmentWidget
, nltk.draw.util.AbstractContainerWidget
, nltk.draw.util.SequenceWidget
, nltk.draw.util.SpaceWidget
, nltk.draw.util.StackWidget
, nltk.draw.util.TextWidget
Returns | |
str | a string representation of this canvas widget. |
nltk.draw.tree.TreeSegmentWidget
, nltk.draw.tree.TreeWidget
, nltk.draw.util.BoxWidget
, nltk.draw.util.BracketWidget
, nltk.draw.util.OvalWidget
, nltk.draw.util.ParenWidget
, nltk.draw.util.SequenceWidget
, nltk.draw.util.StackWidget
, nltk.draw.util.TextWidget
Set the value of the attribute attr to value. See the class documentation for a list of attributes supported by this canvas widget.
Returns | |
None | Undocumented |
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. |
Register a new callback that will be called whenever this CanvasWidget is clicked on.
Parameters | |
callback:function | The callback function that will be called whenever this CanvasWidget is clicked. This function will be called with this CanvasWidget as its argument. |
button:int | Which button the user should use to click on this CanvasWidget. Typically, this should be 1 (left button), 3 (right button), or 2 (middle button). |
Register a new callback that will be called after this CanvasWidget is dragged. This implicitly makes this CanvasWidget draggable.
Parameters | |
callback:function | The callback function that will be called whenever this CanvasWidget is clicked. This function will be called with this CanvasWidget as its argument. |
Returns | |
list of CanvasWidget | A list of the hierarchical children of this canvas widget. These children are considered part of self for purposes of user interaction. |
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 | |
None | Undocumented |
Raises | |
ValueError | if this CanvasWidget has a parent. |
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:int | The number of pixels to move this canvas widget rightwards. |
dy:int | The number of pixels to move this canvas widget downwards. |
Returns | |
None | Undocumented |
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 | |
x | Undocumented |
y | Undocumented |
anchor | The 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,y | The location that the canvas widget should be moved to. |
Returns | |
CanvasWidget or None | The hierarchical parent of this canvas widget. self is considered a subpart of its parent for purposes of user interaction. |
Returns | |
list of int | a list of the canvas tags for all graphical elements managed by this canvas widget, including graphical elements managed by its child widgets. |
Remove a callback that was registered with bind_click.
Parameters | |
button:int | Which button the user should use to click on this CanvasWidget. Typically, this should be 1 (left button), 3 (right button), or 2 (middle button). |
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:CanvasWidget | The child widget that changed. |
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.
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.
- 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.
- Handle a release callback:
- unregister motion & button release callbacks.
- decide whether they clicked, dragged, or cancelled
- call the appropriate handler.
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:CanvasWidget | The new child widget. child must not already have a parent. |
nltk.draw.tree.TreeSegmentWidget
, nltk.draw.tree.TreeWidget
, nltk.draw.util.AbstractContainerWidget
, nltk.draw.util.SequenceWidget
, nltk.draw.util.StackWidget
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 | |
None | Undocumented |
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:CanvasWidget | The child widget to remove. child must be a child of this canvas widget. |
nltk.draw.tree.TreeSegmentWidget
, nltk.draw.tree.TreeWidget
, nltk.draw.util.BoxWidget
, nltk.draw.util.BracketWidget
, nltk.draw.util.OvalWidget
, nltk.draw.util.ParenWidget
, nltk.draw.util.ScrollWatcherWidget
, nltk.draw.util.SequenceWidget
, nltk.draw.util.SpaceWidget
, nltk.draw.util.StackWidget
, nltk.draw.util.TextWidget
Returns | |
list of int | a list of canvas tags for all graphical elements managed by this canvas widget, not including graphical elements managed by its child widgets. |
nltk.draw.tree.TreeSegmentWidget
, nltk.draw.util.BoxWidget
, nltk.draw.util.BracketWidget
, nltk.draw.util.OvalWidget
, nltk.draw.util.ParenWidget
, nltk.draw.util.ScrollWatcherWidget
, nltk.draw.util.SequenceWidget
, nltk.draw.util.StackWidget
Update this canvas widget in response to a change in one of its children.
Parameters | |
child:CanvasWidget | The child that changed. |
Returns | |
None | Undocumented |