canvas
Guide: Canvas
Makes a canvas: an area for shapes, text, images, and widgets that a script draws and moves.
canvas PATHNAME ?OPTION VALUE ...?
Description
Makes a canvas widget with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. A script draws on a canvas with items. create makes an item, and gives its ID, an integer that no other item of the canvas has. Later items show above earlier items, and raise and lower change the order.
The coordinates of the items are distances in the space of the canvas, with x to the right and y down. A scroll changes the part of that space in view, and canvasx and canvasy change a point of the window to a point of the canvas.
Many widget commands take a TAGORID: an item ID, a tag, or an expression of tags. A tag is a name that a script gives to items with -tags or addtag. Each item has the tag all, and the item under the pointer has the tag current. An expression joins tags with &&, ||, ^, !, and parentheses, such as {box && !round}.
The item types are arc, bitmap, image, line, oval, polygon, rectangle, text, and window. The create command lists the options of each type.
The parent of PATHNAME must exist, and PATHNAME must not. A canvas is a Canvas in CSS and for the option database.
Widget commands
| Command | Description |
|---|---|
canvas addtag | Adds TAG to each item that a search finds. |
canvas bbox | Gives the box around the items. |
canvas bind | Binds a script to an event on the items of a tag or an ID. |
canvas canvasx | Changes an x of the window to an x of the canvas. |
canvas canvasy | Changes a y of the window to a y of the canvas. |
canvas coords | Gives or sets the coordinates of an item. |
canvas create | Makes an item of TYPE and gives its ID. |
canvas dchars | Deletes characters of a text item, or points of a line or a polygon. |
canvas delete | Deletes the items of each TAGORID. |
canvas dtag | Removes a tag from the items of TAGORID. |
canvas find | Gives the IDs of the items that a search finds. |
canvas focus | Gives or sets the item that gets the keys. |
canvas gettags | Gives the tags of an item. |
canvas icursor | Moves the insertion cursor of a text item. |
canvas image | Draws the canvas into a photo image. |
canvas imove | Moves one point of a line or a polygon. |
canvas index | Gives the number of a place in a text item, or of a coordinate of a line or a polygon. |
canvas insert | Inserts text into a text item, or points into a line or a polygon. |
canvas itemcget | Gives the value of an option of an item. |
canvas itemconfigure | Gives or sets the options of the items. |
canvas lower | Moves items lower in the order of the display. |
canvas move | Moves the items by a distance. |
canvas moveto | Moves the items so that their box starts at a point. |
canvas postscript | Gives the items as Encapsulated PostScript. |
canvas raise | Moves items higher in the order of the display. |
canvas rchars | Replaces characters of a text item, or coordinates of a line or a polygon. |
canvas scale | Scales the coordinates of the items about a point. |
canvas rotate | Turns the coordinates of the items around a point. |
canvas scan | Scrolls the canvas by a drag of the mouse. |
canvas select | Reads or changes the selected text of a text item. |
canvas type | Gives the type of an item. |
canvas xview | Reads or changes the horizontal view of the canvas. |
canvas yview | Reads or changes the vertical view of the canvas. |
canvas cget | Gives the value of an option of the canvas. |
canvas configure | Gives or sets the options of the canvas. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-background | color | #d9d9d9 | background | The background color of the canvas. |
-borderwidth | screen distance | 0 | borderWidth | The width of the border of the canvas. |
-closeenough | number | 1 | closeEnough | How near the pointer must be to an item to be over it. |
-confine | boolean | 1 | confine | Whether the view stays inside the scroll region. |
-cursor | cursor name | empty | cursor | The shape of the pointer over the canvas. |
-height | screen distance | 7c | height | The height that the canvas asks for. |
-highlightbackground | color | #d9d9d9 | highlightBackground | The color of the focus ring while the canvas has no focus, in the look of Tk on X11. |
-highlightcolor | color | #000000 | highlightColor | The color of the focus ring while the canvas has the focus, in the look of Tk on X11. |
-highlightthickness | screen distance | 1 | highlightThickness | The width of the focus ring, in the look of Tk on X11. |
-insertbackground | color | #000000 | insertBackground | The color of the insertion cursor of a text item. |
-insertborderwidth | screen distance | 0 | insertBorderWidth | The width of the border of the insertion cursor. |
-insertofftime | integer | 300 | insertOffTime | The time in milliseconds that the insertion cursor of a text item is hidden in each blink. |
-insertontime | integer | 600 | insertOnTime | The time in milliseconds that the insertion cursor of a text item shows in each blink. |
-insertwidth | screen distance | 2 | insertWidth | The width of the insertion cursor of a text item. |
-offset | string | 0,0 | offset | The origin of the stipple patterns of the items. |
-relief | relief | flat | relief | The 3-D look of the border. |
-scrollregion | string | empty | scrollRegion | The area of the canvas that the scrollbars can reach. |
-selectbackground | color | #c3c3c3 | selectBackground | The background color of the selected text of a text item. |
-selectborderwidth | screen distance | 1 | selectBorderWidth | The width of the border around the selected text of a text item. |
-selectforeground | color | #000000 | selectForeground | The color of the selected text of a text item. |
-state | state | normal | state | The default state of the items. |
-takefocus | string | empty | takeFocus | Whether the Tab key can move the focus to the canvas. |
-width | screen distance | 10c | width | The width that the canvas asks for. |
-xscrollcommand | script | empty | xScrollCommand | A command prefix that the canvas calls when its horizontal view changes. |
-xscrollincrement | screen distance | 0 | xScrollIncrement | The step of a horizontal scroll by units. |
-yscrollcommand | script | empty | yScrollCommand | A command prefix that the canvas calls when its vertical view changes. |
-yscrollincrement | screen distance | 0 | yScrollIncrement | The step of a vertical scroll by units. |
-background COLOR
The background color of the canvas.
The color behind the content. -bg is the same option. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

-background {#fde68a}
-background {#bfdbfe}-width 120 -height 60.% canvas .background -background {#336699}
> .background cget -background
#336699% canvas .background2 -background nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-borderwidth DISTANCE
The width of the border of the canvas.
The width of the border around the canvas. -bd is the same option. A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.

-borderwidth 0
-borderwidth 1
-borderwidth 4-width 120 -height 60 -relief solid.% canvas .borderwidth -borderwidth 4
> .borderwidth cget -borderwidth
4% canvas .borderwidth2 -borderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-closeenough DISTANCE
How near the pointer must be to an item to be over it.
The pointer is over an item, for the current tag and the bindings of items, when it is at most this many pixels from the item.
% canvas .closeenough -closeenough 2.5
> .closeenough cget -closeenough
2.5% canvas .closeenough2 -closeenough x
expected floating-point number but got "x"
-errorcode TCL VALUE NUMBER-confine BOOLEAN
Whether the view stays inside the scroll region.
In Tk, a true value keeps the view of xview and yview inside -scrollregion. ztk stores the value with no effect.
% canvas .confine -confine 0
> .confine cget -confine
0% canvas .confine2 -confine maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER-cursor CURSOR
The shape of the pointer over the canvas.
Each value side by side: the contact sheet of the cursors
A cursor name of Tk, such as hand2 or xterm. A value that starts with @ names a file. An empty value uses the cursor of the parent.
% canvas .cursor -cursor hand2
> .cursor cget -cursor
hand2% canvas .cursor2 -cursor nocursor
bad cursor spec "nocursor"
-errorcode TK VALUE CURSOR-height DISTANCE
The height that the canvas asks for.
The height of the area of the items, with no border. The default is 7 centimeters.

-height 40
-height 60-width 120.% canvas .height -height 4
> .height cget -height
4% canvas .height2 -height x
bad screen distance "x"
-errorcode TK VALUE PIXELS-highlightbackground COLOR
The color of the focus ring while the canvas has no focus, in the look of Tk on X11.
The color of the highlight ring of -highlightthickness while the canvas does not have the focus. ztk stores the value with no effect.
% canvas .highlightbackground -highlightbackground {#336699}
> .highlightbackground cget -highlightbackground
#336699% canvas .highlightbackground2 -highlightbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-highlightcolor COLOR
The color of the focus ring while the canvas has the focus, in the look of Tk on X11.
The color of the highlight ring of -highlightthickness while the canvas has the focus. ztk stores the value with no effect.
% canvas .highlightcolor -highlightcolor {#336699}
> .highlightcolor cget -highlightcolor
#336699% canvas .highlightcolor2 -highlightcolor nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-highlightthickness DISTANCE
The width of the focus ring, in the look of Tk on X11.
The width of the ring around the canvas that shows the focus. The modern theme draws the focus with the stylesheet.
% canvas .highlightthickness -highlightthickness 4
> .highlightthickness cget -highlightthickness
4% canvas .highlightthickness2 -highlightthickness x
bad screen distance "x"
-errorcode TK VALUE PIXELS-insertbackground COLOR
The color of the insertion cursor of a text item.
The color of the bar that marks the place of the next typed character.
% canvas .insertbackground -insertbackground {#336699}
> .insertbackground cget -insertbackground
#336699% canvas .insertbackground2 -insertbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-insertborderwidth DISTANCE
The width of the border of the insertion cursor.
In Tk, the insertion cursor can have a 3-D border of this width. ztk stores the value with no effect.
% canvas .insertborderwidth -insertborderwidth 4
> .insertborderwidth cget -insertborderwidth
4% canvas .insertborderwidth2 -insertborderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-insertofftime N
The time in milliseconds that the insertion cursor of a text item is hidden in each blink.
0 stops the blink, and the cursor stays on.
% canvas .insertofftime -insertofftime 5
> .insertofftime cget -insertofftime
5% canvas .insertofftime2 -insertofftime x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-insertontime N
The time in milliseconds that the insertion cursor of a text item shows in each blink.
The cursor blinks only while the canvas has the focus and -insertofftime is more than 0.
% canvas .insertontime -insertontime 5
> .insertontime cget -insertontime
5% canvas .insertontime2 -insertontime x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-insertwidth DISTANCE
The width of the insertion cursor of a text item.
A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.
% canvas .insertwidth -insertwidth 4
> .insertwidth cget -insertwidth
4% canvas .insertwidth2 -insertwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-offset OFFSET
The origin of the stipple patterns of the items.
In Tk, the value is X,Y, #X,Y, or an anchor such as nw. ztk stores the value with no effect, because it draws no stipple.
% canvas .offset -offset {4,4}
> .offset cget -offset
4,4ztk stores each value with no check. Tk 8.6.16 gives the error bad offset for a value of another form.
-relief RELIEF
The 3-D look of the border.
Each value side by side: the contact sheet of the reliefs
The style of the border: raised and sunken look 3-D, and groove and ridge are lines. The theme draws the border, and the CSS of the theme can give a relief its own look with border-style.
Values: flat, groove, raised, ridge, solid, sunken.

-relief flat
-relief raised
-relief sunken
-relief groove
-relief ridge
-relief solid-width 120 -height 60 -borderwidth 4.% canvas .relief -relief groove
> .relief cget -relief
groove% canvas .relief2 -relief bumpy
bad relief "bumpy": must be flat, groove, raised, ridge, solid, or sunken
-errorcode TCL LOOKUP INDEX relief bumpy-scrollregion LIST
The area of the canvas that the scrollbars can reach.
A list of four distances: the left, the top, the right, and the bottom of the area. xview and yview give fractions of this area. An empty value uses no scroll region.
% canvas .scrollregion -scrollregion {0 0 1000 800}
> .scrollregion cget -scrollregion
0 0 1000 800% canvas .scrollregion2 -scrollregion {1 2}
bad scrollRegion "1 2"
-errorcode NONE-selectbackground COLOR
The background color of the selected text of a text item.
The color behind the characters of the selection. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.
% canvas .selectbackground -selectbackground {#336699}
> .selectbackground cget -selectbackground
#336699% canvas .selectbackground2 -selectbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-selectborderwidth DISTANCE
The width of the border around the selected text of a text item.
In Tk, the selection can have a 3-D border of this width. ztk stores the value with no effect.
% canvas .selectborderwidth -selectborderwidth 4
> .selectborderwidth cget -selectborderwidth
4% canvas .selectborderwidth2 -selectborderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-selectforeground COLOR
The color of the selected text of a text item.
The color of the characters of the selection.
% canvas .selectforeground -selectforeground {#336699}
> .selectforeground cget -selectforeground
#336699% canvas .selectforeground2 -selectforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-state WORD
The default state of the items.
disabled makes each item with no state of its own use its disabled options and ignore the events of the pointer. An item can set its own -state.
Values: normal, disabled.
% canvas .state -state disabled
> .state cget -state
disabled% canvas .state2 -state bogus
bad state value "bogus": must be normal or disabled
-errorcode TCL LOOKUP INDEX state value bogus-takefocus TEXT
Whether the Tab key can move the focus to the canvas.
1 puts the canvas in the order of the Tab key, and 0 leaves it out. An empty value uses the default rule of Tk for the widget. In Tk the value can also be a script, and its result is the answer. ztk stores each value with no check.
% canvas .takefocus -takefocus 1
> .takefocus cget -takefocus
1ztk stores each value of the option with no check.
-width DISTANCE
The width that the canvas asks for.
The width of the area of the items, with no border. The default is 10 centimeters.

-width 80
-width 120-height 60.% canvas .width -width 4
> .width cget -width
4% canvas .width2 -width x
bad screen distance "x"
-errorcode TK VALUE PIXELS-xscrollcommand SCRIPT
A command prefix that the canvas calls when its horizontal view changes.
The canvas calls the prefix with two fractions, the first and the last visible part of the content, from 0 to 1. The usual value is {.sb set} for a scrollbar .sb.
% canvas .xscrollcommand -xscrollcommand {set ::done 1}
> .xscrollcommand cget -xscrollcommand
set ::done 1ztk stores each value of the option with no check.
-xscrollincrement DISTANCE
The step of a horizontal scroll by units.
xview scroll N units moves the view by N times this distance. 0 or less makes the step a tenth of the width of the canvas.
% canvas .xscrollincrement -xscrollincrement 4
> .xscrollincrement cget -xscrollincrement
4% canvas .xscrollincrement2 -xscrollincrement x
bad screen distance "x"
-errorcode TK VALUE PIXELS-yscrollcommand SCRIPT
A command prefix that the canvas calls when its vertical view changes.
The canvas calls the prefix with two fractions, the first and the last visible part of the content, from 0 to 1. The usual value is {.sb set} for a scrollbar .sb.
% canvas .yscrollcommand -yscrollcommand {set ::done 1}
> .yscrollcommand cget -yscrollcommand
set ::done 1ztk stores each value of the option with no check.
-yscrollincrement DISTANCE
The step of a vertical scroll by units.
yview scroll N units moves the view by N times this distance. 0 or less makes the step a tenth of the height of the canvas.
% canvas .yscrollincrement -yscrollincrement 4
> .yscrollincrement cget -yscrollincrement
4% canvas .yscrollincrement2 -yscrollincrement x
bad screen distance "x"
-errorcode TK VALUE PIXELSReturns
PATHNAME.
Examples
% canvas .draw -width 200 -height 100
> .draw create rectangle 10 10 60 40 -fill red -tags box
> .draw create oval 70 10 120 60 -tags {box round}
> .draw find withtag box
1 2
% canvas .move
> .move create line 0 0 50 50 -width 2
> .move move 1 10 0
> .move coords 1
10.0 0.0 60.0 50.0
% winfo class [canvas .go]
Canvas
Errors
% canvas .x -foo 1
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo
% canvas .twice
> canvas .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% canvas .no.b
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% canvas
wrong # args: should be "canvas pathName ?-option value ...?"
-errorcode TCL WRONGARGS
Differences from Tcl
postscriptwrites ztk's own prolog and drawing: the page has the same place, size, and box as in Tk, but other PostScript procedures. A text uses Helvetica, Times, or Courier, chosen from the family, unless-fontmapnames a font. A window item, a stipple, and the underline of a text are absent from the page, and an outline with a stipple prints solid.- Items that keep the default black of Tk render with the text color of the theme, so they stay visible in the dark scheme.
itemcgetstill gives#000000. - A
createthat fails uses no ID. In Tk 8.6.16 the next item gets a higher ID. find enclosedleaves out an item that touches the edge of the area. For example,find enclosed 0 0 55 35does not find the polygon0 0 10 0 5 10, and Tk 8.6.16 finds it.- A tag expression has at most 256 levels of
!and of parentheses. More give the errortoo many nested levels in tag search expression. -offsetaccepts each value. Tk 8.6.16 gives the errorbad offsetfor a value that is notX,Y,#X,Y, or an anchor.- An error of a widget command has the
-errorcodeNONE. Tk 8.6.16 gives a code such asTK LOOKUP CANVAS_ITEM_TYPE,TK VALUE PIXELS, orTCL WRONGARGS. -confine,-insertborderwidth,-selectborderwidth,-highlightbackground, and-highlightcolorare stored with no effect.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1