Canvas#
Canvas is tkinter’s drawing surface (tk.Canvas) — a 2-D area that holds
items (lines, shapes, text, images, embedded widgets) you create, move,
restyle, and delete by id or tag. It is themed by ttkbootstrap. This page is the
complete reference for its own options and methods; the shared widget methods are
under Capabilities.
Options#
Option |
Type |
Description |
|---|---|---|
|
|
Constructor only. |
|
|
The surface color. |
|
|
The requested width in pixels. |
|
|
The requested height in pixels. |
|
|
The 3-D border width in pixels. |
|
|
The border style: |
|
|
The width of the focus highlight around the widget. |
|
|
The focus-highlight color when the widget has focus. |
|
|
The focus-highlight color when the widget does not have focus. |
|
|
The scrollable area as |
|
|
Whether to keep the view inside |
|
|
A callback connecting the canvas to a horizontal scrollbar. |
|
|
A callback connecting the canvas to a vertical scrollbar. |
|
|
The step size for horizontal “unit” scrolling, in pixels. |
|
|
The step size for vertical “unit” scrolling, in pixels. |
|
|
The background of selected text within an editable text item. |
|
|
The foreground of selected text within an editable text item. |
|
|
The border width of the selection, in pixels. |
|
|
The color of the text insert cursor. |
|
|
The width of the insert cursor, in pixels. |
|
|
The border width of the insert cursor, in pixels. |
|
|
The insert-cursor blink on-time, in milliseconds. |
|
|
The insert-cursor blink off-time, in milliseconds. |
|
|
The default item state: |
|
|
How near (in pixels) the pointer must be to count as “over” an item. |
|
|
The origin for tiled stipple/fill patterns. |
|
|
The mouse cursor over the canvas (see Cursors). |
|
|
Whether the canvas accepts keyboard focus. |
Methods#
Creating items#
Each create_* takes the item’s coordinates followed by keyword options and
returns the new item’s integer id. Common options: fill, outline,
width, stipple, dash, state, and tags.
- create_line(x0, y0, x1, y1, ..., **options)
Draw a line (or multi-point path) through the given points.
- Parameters:
options –
fill,width,dash,arrow("first"/"last"/"both"),smooth,capstyle,joinstyle,tags.- Returns:
the new item id.
- Return type:
- create_rectangle(x0, y0, x1, y1, **options)
Draw a rectangle from two opposite corners.
- Parameters:
options –
fill,outline,width,dash,state,tags.- Returns:
the new item id.
- Return type:
- create_oval(x0, y0, x1, y1, **options)
Draw an ellipse bounded by the given rectangle.
- Returns:
the new item id.
- Return type:
- create_polygon(x0, y0, x1, y1, ..., **options)
Draw a closed polygon through three or more points.
- Parameters:
options –
fill,outline,width,smooth,tags.- Returns:
the new item id.
- Return type:
- create_arc(x0, y0, x1, y1, **options)
Draw an arc, chord, or pie slice within the bounding rectangle.
- Parameters:
options –
start(degrees),extent(degrees),style("pieslice"/"chord"/"arc"),fill,outline,tags.- Returns:
the new item id.
- Return type:
- create_text(x, y, **options)
Place a string of text at a point.
- Parameters:
options –
text,font,fill,anchor,justify,width(wrap),angle,tags.- Returns:
the new item id.
- Return type:
- create_image(x, y, **options)
Place an image at a point.
- Parameters:
options –
image(aPhotoImage),anchor,state,tags.- Returns:
the new item id.
- Return type:
- create_bitmap(x, y, **options)
Place a bitmap at a point.
- Parameters:
options –
bitmap,anchor,foreground,background,tags.- Returns:
the new item id.
- Return type:
- create_window(x, y, **options)
Embed a child widget on the canvas at a point.
- Parameters:
options –
window(an existing widget),anchor,width,height,tags.- Returns:
the new item id.
- Return type:
Coordinates and geometry#
- coords(tagOrId, *coords)
Get or set an item’s coordinates.
- Parameters:
tagOrId – the item to address.
coords – new coordinates to set; omit to query.
- Returns:
the current coordinates (flat list) when queried, else
None.- Return type:
list | None
- move(tagOrId, dx, dy)
Shift matching items by an offset.
- Parameters:
dx – horizontal delta, in pixels.
dy – vertical delta, in pixels.
- Returns:
None.
- moveto(tagOrId, x='', y='')
Move matching items so their top-left is at an absolute position.
- Returns:
None.
- scale(tagOrId, xOrigin, yOrigin, xScale, yScale)
Scale matching items about an origin point.
- Parameters:
xScale – horizontal scale factor.
yScale – vertical scale factor.
- Returns:
None.
- bbox(*tagsOrIds)
Return the bounding box that encloses the matching items.
- Returns:
(x1, y1, x2, y2)in pixels, orNoneif nothing matches.- Return type:
tuple | None
- canvasx(screenx, gridspacing=None)
Convert a window x coordinate to a canvas x coordinate (accounting for scrolling), optionally snapped to a grid.
- Return type:
- canvasy(screeny, gridspacing=None)
Vertical counterpart of
canvasx().- Return type:
Configuring items#
- itemconfigure(tagOrId, **options)
Set (or query) options on matching items. Alias:
itemconfig.- Parameters:
tagOrId – the item(s) to change.
options – any options valid for the item type (
fill,outline,width,text,state,tags, …).
- Returns:
the option spec when queried with a single option name, else
None.
- itemcget(tagOrId, option)
Return one option of the first matching item.
- type(tagOrId)
Return the type of the first matching item.
- Returns:
"line","rectangle","oval","polygon","arc","text","image","bitmap", or"window".- Return type:
Finding items#
- find_above(tagOrId)
Return the id of the item just above the topmost matching item in the stacking order.
find_below(tagOrId)is the downward counterpart.
- find_closest(x, y, halo=None, start=None)
Return the single item nearest a point (topmost on ties).
- find_overlapping(x1, y1, x2, y2)
Return the ids of items that overlap the given rectangle.
Tagging#
- addtag_withtag(newtag, tagOrId)
Add a tag to every item matching
tagOrId. (Companions select by position:addtag_above/_below/_closest/_enclosed/_overlapping/_all.)- Returns:
None.
- dtag(tagOrId, tagToDelete=None)
Remove a tag from matching items (the items themselves remain).
- Returns:
None.
Deleting#
- delete(*tagsOrIds)
Delete every matching item from the canvas.
- Returns:
None.
Item events and stacking#
- tag_bind(tagOrId, sequence=None, func=None, add=None)
Bind an event to items — the way to make canvas items clickable/hoverable.
- Parameters:
sequence – an event sequence, e.g.
"<Button-1>".func – the callback, receiving the event object.
add –
"+"to add rather than replace.
- Returns:
a binding id.
- Return type:
- tag_unbind(tagOrId, sequence, funcid=None)
Remove an item binding.
- Returns:
None.
- tag_raise(tagOrId, aboveThis=None)
Raise matching items in the stacking order (drawn on top). Alias:
lift.- Returns:
None.
- tag_lower(tagOrId, belowThis=None)
Lower matching items in the stacking order. Alias:
lower.- Returns:
None.
Editing text items#
These act on editable text (and window) items, using a character index
or the special indices "insert", "end", "sel.first"/"sel.last".
- insert(tagOrId, index, string)
Insert text into an item at a character index.
- Returns:
None.
- dchars(tagOrId, first, last=None)
Delete characters from an item between two indices.
- Returns:
None.
- index(tagOrId, index)
Resolve a text-item index expression to an integer character position.
- Return type:
- icursor(tagOrId, index)
Move the insert cursor within a text item.
- Returns:
None.
- select_from(tagOrId, index)
Anchor the selection inside a text item at
index; extend it withselect_to(tagOrId, index)andselect_adjust(tagOrId, index).select_clear()removes the selection, andselect_item()returns the id of the item holding it (orNone).- Returns:
None.
Scrolling#
- xview(*args)
Query or set the horizontal view; usually wired to a scrollbar via
xscrollcommand.yviewis the vertical counterpart, and both have*_moveto(fraction)and*_scroll(number, what)variants.- Returns:
with no args, the visible fraction
(first, last); elseNone.- Return type:
tuple | None
- scan_mark(x, y)
Record a starting point for a fast drag-scroll (paired with
scan_dragto).- Returns:
None.
- scan_dragto(x, y, gain=10)
Scroll the view relative to the
scan_markpoint, multiplied bygain.- Returns:
None.
Export#
- postscript(**options)
Render the canvas (or a region of it) to PostScript.
- Parameters:
options –
file=a path orNoneto return the text;x/y/width/heightfor a region;colormode("color"/"gray"/"mono");pagewidth/pageheight.- Returns:
the PostScript text when no
fileis given, elseNone.- Return type:
str | None
See also#
Tk canvas manual page — the canonical upstream reference (Tcl 8.6).