tkCanvasGraph package¶
tkCanvasGraph.canvas module¶
Canvas graphs.
This module defines canvas graphs. Such a canvas is a TK canvas on which we can display graphs.
-
class
tkCanvasGraph.canvas.
CanvasGraph
(parent, **config)¶ Bases:
sphinx.ext.autodoc.Canvas
A canvas graph is a TK canvas on which you can display graphs.
-
add_edge
(edge, position=None)¶ Add the given edge on this canvas at position, if specified. If position is None, set the label at the middle position between its origin and end vertices.
Parameters: - edge – the edge to add and draw;
- position – if not None, an x,y tuple.
-
add_vertex
(vertex, position=None)¶ Add the given vertex on this canvas at position, if specified. If position is None, set it at random in the frame defined by the existing elements, or in the (0,0), (100, 100) rectangle if there are no elements.
Parameters: - vertex – the vertex to add and draw;
- position – if not None, an x,y tuple.
-
apply_interactive_layout
(layout)¶ Apply the given interactive layout.
Parameters: layout – the layout to apply now and then. The interval the layout is applied is self.layout_interval. The self.layouting variable is set to True to tell that interactive layouting is enabled. Then every layout_interval milliseconds, the given layout is applied on the current graph. The process is stopped as soon as layouting is set to False.
-
apply_layout
(layout)¶ Apply the given layout on this canvas.
Parameters: layout – the layout to apply, must comply with the apply method (see layout.Layout).
-
delete_element
(element)¶ Delete the given element.
Parameters: element – the element to delete.
-
element_by_handle
(handle)¶ Return the element attached to the given handle, if any; None otherwise.
Parameters: handle – the handle to get. Returns: the element corresponding to the given handle, if any, otherwise None.
-
move_elements
(elements, dx, dy)¶ Move the given elements of (dx,dy) offset.
Parameters: - elements – the iterable of elements to move.
- dx – the horizontal offset.
- dy – the vertical offset.
-
refresh
()¶ Refresh all elements of this canvas.
-
register_mouse
(mouse, button, modifiers='')¶ Register a new mouse for button pressed with modifiers.
Parameters: - mouse – the mouse to register. Must comply with the mouse.Mouse methods.
- button – the button to react to.
- modifiers – the modifiers to activate the mouse for.
-
register_transformer
(transformer)¶ Register the given transformer. The transformer must be a function taking an element (vertex or edge) and a style as arguments and updating the style.
Parameters: transformer – the transformer to register. The transformer has access to and can change:
- the label of the element: style[“label”]. The transformer should not delete the “label” key and its value should be a string.
- the shape of the element: style[“shape”]. The transformer should not delete the “shape” key and its value should be a valid shape (sub-class of graph.Shape).
- the style of label: style[“label_style”]. The style of the label should be a dictionary of valid tkinter canvas text configurations.
- the style of the shape: style[“shape_style”]. The style of the shape should be a dictionary of valid tkinter canvas shape configurations.
The canvas is refreshed.
-
unregister_mouse
(mouse, button, modifiers='')¶ Unregister a registered mouse for button pressed with modifiers.
Parameters: - mouse – the mouse to unregister.
- button – the button for which the mouse was registered.
- modifiers – the modifiers for which the mouse was registered.
-
unregister_transformer
(transformer)¶ Unregister the given transformer.
Parameters: transformer – the transformer to unregister. The canvas is refreshed.
-
-
class
tkCanvasGraph.canvas.
InteractiveCanvasGraph
(parent, **config)¶ Bases:
tkCanvasGraph.canvas.CanvasGraph
A selectable canvas graph is a TK canvas on which you can display graphs. In addition, such a canvas graph embeds the necessary mouses to select and move around elements of the displayed graph.
Interactive canvas graphs have an observable set of selected elements (canvas.selected) that can be observed to be notified when its content changes.
-
class
tkCanvasGraph.canvas.
CanvasFrame
(parent, **config)¶ Bases:
sphinx.ext.autodoc.Frame
A frame containing a canvas graph in which one can display a graph. The frame also includes buttons to apply layouts (interactive, force-based and dot layouts), as well as scrollbars and mouse wheel scroll. The canvas graph is selectable, in the sense that it contains necessary mechanisms to select and move elements.
tkCanvasGraph.exception module¶
-
exception
tkCanvasGraph.exception.
CanvasGraphError
¶ Bases:
Exception
A generic CanvasGraph error.
tkCanvasGraph.graph module¶
Graph elements.
This module defines the elements drawn on canvas graph: vertices and edges.
-
class
tkCanvasGraph.graph.
Vertex
(canvas, label='', tooltip=None)¶ Bases:
tkCanvasGraph.graph.GraphElement
A vertex of a graph.
In addition to the common style of graph elements, vertices define a style for their selected states. The style.selected dictionary contains:
- shape, defining the appearance of the shape of the vertex when selected;
- text, defining the appearance of the label of the vertex when selected.
Note that any style that is not overriden is kept as-is. For example, if style.selected changes the width of the border of the vertex, and style.common does not define a width, then the width will change when selecting the vertex, but will not change back when deselecting it.
-
bbox
¶ Return the bounding box of this element. :return: the (x0, y0, x1, y1) coordinates of the bounding box.
This element must be already drawn on its canvas.
-
bind
(event, callback, add=None)¶ Add an event binding to this element on canvas.
Parameters: - event – the event specifier;
- callback – the function to call when the event occurs. a function taking one argument: the event;
- add – if not None and set to “+”, the new binding is added to any existing binding.
This element must be already drawn on its canvas.
-
center
¶ Return the center point of this element.
Returns: the (x, y) coordinates of the center point. This element must be already drawn on its canvas.
-
delete_handles
()¶ Set the handles of this element to None.
-
dimensions
¶ Return this element width and height.
Returns: the (width, height) pair. This element must be already drawn on its canvas.
-
draw
(x, y)¶ Draw this element on its canvas, centered at position x, y.
Parameters: - x – the horizontal position;
- y – the vertical position.
-
handles
¶ Return the handles of this element.
-
intersection
(end, bbox=None)¶ Return the point of intersection between this element if it had the given bounding box, and the line segment defined by the center of this bounding box and end. Return None is such an intersection does not exist.
Parameters: - end – the (x, y) coordinates of the ending point;
- bbox – if not None, the tkinter-style bounding box to consider.
Returns: the (x, y) coordinates of the intersection point of this element and the line between its center and end, if any, None otherwise.
If bbox is None, use the actual element bounding box.
-
label
¶ The label of this element.
-
move
(dx, dy)¶ Move this vertex on canvas.
Parameters: - dx – the difference to move on x axis;
- dy – the difference to move on y axis.
This element must be already drawn on its canvas.
-
move_to
(x, y)¶ Move this vertex to x,y on canvas and return the corresponding dx,dy.
Parameters: - x – the horizontal position;
- y – the vertical position.
Returns: the difference of old and new positions.
This element must be already drawn on its canvas.
-
refresh
()¶ Refresh the appearance of this element on canvas.
This element must be already drawn on its canvas.
-
shape
¶ The shape of this element.
-
unbind
(event)¶ Remove all the bindings for event of the element on canvas.
Parameters: event – the event specifier to remove the binding on. This element must be already drawn on its canvas.
-
class
tkCanvasGraph.graph.
Edge
(canvas, origin, end, label='', tooltip=None)¶ Bases:
tkCanvasGraph.graph.GraphElement
An edge of a graph.
In addition to the common style of graph elements, edges define the style.common.arrow dictionary for the arrow of the edge.
-
bbox
¶ Return the bounding box of this element. :return: the (x0, y0, x1, y1) coordinates of the bounding box.
This element must be already drawn on its canvas.
-
bind
(event, callback, add=None)¶ Add an event binding to this element on canvas.
Parameters: - event – the event specifier;
- callback – the function to call when the event occurs. a function taking one argument: the event;
- add – if not None and set to “+”, the new binding is added to any existing binding.
This element must be already drawn on its canvas.
-
center
¶ Return the center point of this element.
Returns: the (x, y) coordinates of the center point. This element must be already drawn on its canvas.
-
delete_handles
()¶ Set the handles of this edge to None.
-
dimensions
¶ Return this element width and height.
Returns: the (width, height) pair. This element must be already drawn on its canvas.
-
draw
(x, y)¶
-
handles
¶ Return the handles of this edge.
Returns: the set of handles taking part in this edge.
-
intersection
(end, bbox=None)¶ Return the point of intersection between this element if it had the given bounding box, and the line segment defined by the center of this bounding box and end. Return None is such an intersection does not exist.
Parameters: - end – the (x, y) coordinates of the ending point;
- bbox – if not None, the tkinter-style bounding box to consider.
Returns: the (x, y) coordinates of the intersection point of this element and the line between its center and end, if any, None otherwise.
If bbox is None, use the actual element bounding box.
-
label
¶ The label of this element.
-
move
(dx, dy)¶ Move this vertex on canvas.
Parameters: - dx – the difference to move on x axis;
- dy – the difference to move on y axis.
This element must be already drawn on its canvas.
-
move_to
(x, y)¶ Move this vertex to x,y on canvas and return the corresponding dx,dy.
Parameters: - x – the horizontal position;
- y – the vertical position.
Returns: the difference of old and new positions.
This element must be already drawn on its canvas.
-
refresh
()¶
-
shape
¶ The shape of this element.
-
unbind
(event)¶ Remove all the bindings for event of the element on canvas.
Parameters: event – the event specifier to remove the binding on. This element must be already drawn on its canvas.
-
tkCanvasGraph.layout module¶
-
class
tkCanvasGraph.layout.
Layout
¶ Bases:
object
A graph layout.
-
apply
(canvas, vertices, edges, fixed=None)¶ Apply this layout on canvas. Move the given vertices and edges at their new position.
Parameters: - canvas – the canvas on which operate;
- vertices – the set of vertices to move;
- edges – the set of edges to move;
- fixed – a set of elements that must remain at given position.
-
-
class
tkCanvasGraph.layout.
OneStepForceBasedLayout
¶ Bases:
tkCanvasGraph.layout.Layout
A force-based layout. Applying only cause one step of the computation of the layout. Useful for interactive layout, each application giving the new positions to draw.
-
apply
(canvas, vertices, edges, fixed=None)¶
-
-
class
tkCanvasGraph.layout.
ForceBasedLayout
¶ Bases:
tkCanvasGraph.layout.OneStepForceBasedLayout
A force-based layout. One application gives the final positions.
-
apply
(canvas, vertices, edges, fixed=None)¶
-
tkCanvasGraph.mouse module¶
Mouses.
Mouses handle pressing and releasing a button, with access to the canvas and its elements, as well as moving the pointer on the canvas.
In addition to the abstract mouse that does nothing, this module gives access to several pre-defined mouses.
-
class
tkCanvasGraph.mouse.
Mouse
¶ Bases:
object
A generic mouse doing nothing.
Mouses can be registered to canvas. Three events are handled by mouses:
- pressed button
- moved pointer
- released button
Whenever such an event occurs, the canvas delegates it to the registered mouses.
-
moved
(event)¶ React to mouse moved.
Parameters: event – the mouse event of the moved mouse. Returns: a True value if the event must be passed to the next mouse, a False value otherwise.
-
pressed
(event)¶ React to mouse button pressed.
Parameters: event – the mouse event of the pressed button. Returns: a True value if the event must be passed to the next mouse, a False value otherwise.
-
released
(event)¶ React to mouse button released.
Parameters: event – the mouse event of the released button. Returns: a True value if the event must be passed to the next mouse, a False value otherwise.
-
class
tkCanvasGraph.mouse.
SelectingMouse
(selection=None, elements=None)¶ Bases:
tkCanvasGraph.mouse.Mouse
Add selecting behaviour to a canvas.
- The added behaviours are:
- select individual elements by clicking on them;
- select several elements by drawing a box around them.
-
class
tkCanvasGraph.mouse.
SelectionModifyingMouse
(selection=None, elements=None)¶ Bases:
tkCanvasGraph.mouse.Mouse
Add selection modifications to a canvas.
The added behaviour is adding/removing elements from selection when they are clicked.
-
class
tkCanvasGraph.mouse.
MovingMouse
(selected)¶ Bases:
tkCanvasGraph.mouse.Mouse
Add moving behaviour to a canvas.
The added behaviour is moving the selection when dragged.
-
class
tkCanvasGraph.mouse.
CreatingMouse
(vertices)¶ Bases:
tkCanvasGraph.mouse.Mouse
Add creation behaviours to a canvas.
- The added behaviours are:
- creating a vertex when clicking on empty space;
- creating an edge between two different vertices when dragging from one vertex to another.
-
class
tkCanvasGraph.mouse.
MouseEvent
(canvas, element, position, absolute, number, type_)¶ Bases:
object
The event captured by a mouse.
Such an event has:
- the canvas the event occurred on;
- the current mouse pointer position on the canvas;
- the current mouse pointer position relative to the upper left corner of the screen;
- the button number;
- the type of the event;
- the element of the graph the mouse pointer is on, if any (None otherwise)
tkCanvasGraph.shape module¶
Shapes.
This module defines shapes for graph elements.
-
class
tkCanvasGraph.shape.
Shape
¶ Bases:
object
The shape of a graph element.
-
dimension
(bbox)¶ Return the dimension of this shape around the given bounding box.
Parameters: bbox – the (x0, y0, x1, y1) bounding box. Returns: the (x‘0, y‘0, x‘1, y‘1) bounding box of this shape around bbox.
-
draw
(canvas, bbox, style)¶ Draw this shape on canvas, around the given bounding box, with style, and return the handle on canvas.
Parameters: - canvas – the canvas to draw on;
- bbox – the tkinter-style bounding box to draw around;
- style – the style of the shape.
Returns: the handle of the drawn shape.
-
intersection
(bbox, end)¶ Return the point of intersection between this shape with given bounding box, and the line segment defined by the center of this bounding box and end. Return None is such an intersection does not exist.
Parameters: - bbox – the tkinter-style bounding box of this shape;
- end – the (x, y) coordinates of the ending point.
Returns: the (x, y) coordinates of the intersection point of this shape and the line between its center and end, if any, None otherwise.
-
-
class
tkCanvasGraph.shape.
Oval
(diameter=20)¶ Bases:
tkCanvasGraph.shape.Shape
The Oval shape is an ellipsis shape.
-
dimension
(bbox)¶
-
draw
(canvas, bbox, style)¶
-
intersection
(bbox, end)¶
-
-
class
tkCanvasGraph.shape.
Rectangle
(size=5)¶ Bases:
tkCanvasGraph.shape.Shape
A rectangle shape.
-
dimension
(bbox)¶
-
draw
(canvas, bbox, style)¶
-
intersection
(bbox, end)¶
-
tkCanvasGraph.util module¶
-
class
tkCanvasGraph.util.
ObservableSet
(*args, **kwargs)¶ Bases:
set
An observable set is a set that notifies all its registered observers each time the set of modified.
-
notify
()¶ Force this set to notify all its observers.
-
register
(observer)¶ Register the observer of this set. The observer will be notified through its “update” method every time the set is modified.
Parameters: observer – the observer; supports the “update(set)” method.
-
unregister
(observer)¶ Unregister the given observer. It will not be notified any more.
Parameters: observer – the observer to remove.
-
-
class
tkCanvasGraph.util.
AttrDict
(*args, **kwargs)¶ Bases:
dict
A dictionary where keys can be accessed as attributes.
-
class
tkCanvasGraph.util.
CanvasToolTip
(canvas, handle, text='Your text here', delay=500, **opts)¶ Bases:
object
Modified from http://tkinter.unpythonic.net/wiki/ToolTip
Michael Lange <klappnase (at) freakmail (dot) de>
The ToolTip class provides a flexible tooltip widget for tkinter; it is based on IDLE’s ToolTip module which unfortunately seems to be broken (at least the version I saw).
INITIALIZATION OPTIONS:
- anchor
- where the text should be positioned inside the widget, must be on of “n”, “s”, “e”, “w”, “nw” and so on; default is “center”
- bd
- borderwidth of the widget; default is 1 (NOTE: don’t use “borderwidth” here)
- bg
- background color to use for the widget; default is “lightyellow” (NOTE: don’t use “background”)
- delay
- time in ms that it takes for the widget to appear on the screen when the mouse pointer has entered the parent widget; default is 1500
- fg
- foreground (i.e. text) color to use; default is “black” (NOTE: don’t use “foreground”)
- follow_mouse
- if set to 1 the tooltip will follow the mouse pointer instead of being displayed outside of the parent widget; this may be useful if you want to use tooltips for large widgets like listboxes or canvases; default is 0
- font
- font to use for the widget; default is system specific
- justify
- how multiple lines of text will be aligned, must be “left”, “right” or “center”; default is “left”
- padx
- extra space added to the left and right within the widget; default is 4
- pady
- extra space above and below the text; default is 2
- relief
- one of “flat”, “ridge”, “groove”, “raised”, “sunken” or “solid”; default is “solid”
- state
- must be “normal” or “disabled”; if set to “disabled” the tooltip will not appear; default is “normal”
- text
- the text that is displayed inside the widget
- textvariable
- if set to an instance of tkinter.StringVar() the variable’s value will be used as text for the widget
- width
- width of the widget; the default is 0, which means that “wraplength” will be used to limit the widgets width
- wraplength
- limits the number of characters in each line; default is 150
WIDGET METHODS:
- configure(**opts)
- change one or more of the widget’s options as described above; the changes will take effect the next time the tooltip shows up; NOTE: follow_mouse cannot be changed after widget initialization
Other widget methods that might be useful if you want to subclass ToolTip:
- enter()
- callback when the mouse pointer enters the parent widget
- leave()
- called when the mouse pointer leaves the parent widget
- motion()
- is called when the mouse pointer moves inside the parent widget if follow_mouse is set to 1 and the tooltip has shown up to continually update the coordinates of the tooltip window
- coords()
- calculates the screen coordinates of the tooltip window
- create_contents()
- creates the contents of the tooltip window (by default a tkinter.Label)