ItemsCanvas

class ttkwidgets.ItemsCanvas(*args, **kwargs)[source]

Bases: ttk.Frame

A ttk.Frame containing a Canvas upon which text items can be placed with a coloured background.

The items can be moved around and deleted. A background can also be set.

__init__(*args, **kwargs)[source]

Create an ItemsCanvas.

Parameters:
  • canvaswidth (int) – width of the canvas in pixels
  • canvasheight – height of the canvas in pixels
  • callback_add (function) – callback for when an item is created, *(int item, int rectangle)
  • callback_del (function) – callback for when an item is deleted, *(int item, int rectangle)
  • callback_move (function) – callback for when an item is moved, *(int item, int rectangle, int x, int y)
  • function_new (function) – user defined function for when an item is created, *(self.add_item)
add_item(text, font=('default', 12, 'bold'), backgroundcolor='yellow', textcolor='black', highlightcolor='blue')[source]

Add a new item on the Canvas.

Parameters:
  • text (str) – text to display
  • font (tuple or Font) – font of the text
  • backgroundcolor (str) – background color
  • textcolor (str) – text color
  • highlightcolor (str) – the color of the text when the item is selected
cget(key)[source]

Query widget option.

Parameters:key (str) – option name
Returns:value of the option

To get the list of options for this widget, call the method keys().

config(**kwargs)[source]

Configure resources of the widget.

To get the list of options for this widget, call the method keys(). See __init__() for a description of the widget specific option.

configure(**kwargs)

Configure resources of the widget.

To get the list of options for this widget, call the method keys(). See __init__() for a description of the widget specific option.

del_item()[source]

Delete the current item on the Canvas.

grid_widgets()[source]

Put the widgets in the correct position.

left_motion(event)[source]

Callback for the B1-Motion event, or the dragging of an item.

Moves the item to the desired location, but limits its movement to a place on the actual Canvas. The item cannot be moved outside of the Canvas.

Parameters:event – Tkinter event
left_press(event)[source]

Callback for the press of the left mouse button.

Selects a new item and sets its highlightcolor.

Parameters:event – Tkinter event
left_release(event)[source]

Callback for the release of the left button.

Parameters:event – Tkinter event
right_press(event)[source]

Callback for the right mouse button event to pop up the correct menu.

Parameters:event – Tkinter event
set_background(image=None, path=None, resize=True)[source]

Set the background image of the Canvas.

Parameters:
  • image (PhotoImage) – background image
  • path (str) – background image path
  • resize (bool) – whether to resize the image to the Canvas size