beetlebug▾
/

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

CommandDescription
canvas addtagAdds TAG to each item that a search finds.
canvas bboxGives the box around the items.
canvas bindBinds a script to an event on the items of a tag or an ID.
canvas canvasxChanges an x of the window to an x of the canvas.
canvas canvasyChanges a y of the window to a y of the canvas.
canvas coordsGives or sets the coordinates of an item.
canvas createMakes an item of TYPE and gives its ID.
canvas dcharsDeletes characters of a text item, or points of a line or a polygon.
canvas deleteDeletes the items of each TAGORID.
canvas dtagRemoves a tag from the items of TAGORID.
canvas findGives the IDs of the items that a search finds.
canvas focusGives or sets the item that gets the keys.
canvas gettagsGives the tags of an item.
canvas icursorMoves the insertion cursor of a text item.
canvas imageDraws the canvas into a photo image.
canvas imoveMoves one point of a line or a polygon.
canvas indexGives the number of a place in a text item, or of a coordinate of a line or a polygon.
canvas insertInserts text into a text item, or points into a line or a polygon.
canvas itemcgetGives the value of an option of an item.
canvas itemconfigureGives or sets the options of the items.
canvas lowerMoves items lower in the order of the display.
canvas moveMoves the items by a distance.
canvas movetoMoves the items so that their box starts at a point.
canvas postscriptGives the items as Encapsulated PostScript.
canvas raiseMoves items higher in the order of the display.
canvas rcharsReplaces characters of a text item, or coordinates of a line or a polygon.
canvas scaleScales the coordinates of the items about a point.
canvas rotateTurns the coordinates of the items around a point.
canvas scanScrolls the canvas by a drag of the mouse.
canvas selectReads or changes the selected text of a text item.
canvas typeGives the type of an item.
canvas xviewReads or changes the horizontal view of the canvas.
canvas yviewReads or changes the vertical view of the canvas.
canvas cgetGives the value of an option of the canvas.
canvas configureGives or sets the options of the canvas.

Options

OptionTypeDefaultDatabase nameDescription
-backgroundcolor#d9d9d9backgroundThe background color of the canvas.
-borderwidthscreen distance0borderWidthThe width of the border of the canvas.
-closeenoughnumber1closeEnoughHow near the pointer must be to an item to be over it.
-confineboolean1confineWhether the view stays inside the scroll region.
-cursorcursor nameemptycursorThe shape of the pointer over the canvas.
-heightscreen distance7cheightThe height that the canvas asks for.
-highlightbackgroundcolor#d9d9d9highlightBackgroundThe color of the focus ring while the canvas has no focus, in the look of Tk on X11.
-highlightcolorcolor#000000highlightColorThe color of the focus ring while the canvas has the focus, in the look of Tk on X11.
-highlightthicknessscreen distance1highlightThicknessThe width of the focus ring, in the look of Tk on X11.
-insertbackgroundcolor#000000insertBackgroundThe color of the insertion cursor of a text item.
-insertborderwidthscreen distance0insertBorderWidthThe width of the border of the insertion cursor.
-insertofftimeinteger300insertOffTimeThe time in milliseconds that the insertion cursor of a text item is hidden in each blink.
-insertontimeinteger600insertOnTimeThe time in milliseconds that the insertion cursor of a text item shows in each blink.
-insertwidthscreen distance2insertWidthThe width of the insertion cursor of a text item.
-offsetstring0,0offsetThe origin of the stipple patterns of the items.
-reliefreliefflatreliefThe 3-D look of the border.
-scrollregionstringemptyscrollRegionThe area of the canvas that the scrollbars can reach.
-selectbackgroundcolor#c3c3c3selectBackgroundThe background color of the selected text of a text item.
-selectborderwidthscreen distance1selectBorderWidthThe width of the border around the selected text of a text item.
-selectforegroundcolor#000000selectForegroundThe color of the selected text of a text item.
-statestatenormalstateThe default state of the items.
-takefocusstringemptytakeFocusWhether the Tab key can move the focus to the canvas.
-widthscreen distance10cwidthThe width that the canvas asks for.
-xscrollcommandscriptemptyxScrollCommandA command prefix that the canvas calls when its horizontal view changes.
-xscrollincrementscreen distance0xScrollIncrementThe step of a horizontal scroll by units.
-yscrollcommandscriptemptyyScrollCommandA command prefix that the canvas calls when its vertical view changes.
-yscrollincrementscreen distance0yScrollIncrementThe 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.

canvas with -width 120 -height 60 and -background {#fde68a}-background {#fde68a}canvas with -width 120 -height 60 and -background {#bfdbfe}-background {#bfdbfe}
Each with -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.

canvas with -width 120 -height 60 -relief solid and -borderwidth 0-borderwidth 0canvas with -width 120 -height 60 -relief solid and -borderwidth 1-borderwidth 1canvas with -width 120 -height 60 -relief solid and -borderwidth 4-borderwidth 4
Each with -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.

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.

canvas with -width 120 and -height 40-height 40canvas with -width 120 and -height 60-height 60
Each with -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,4

ztk 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.

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.

canvas with -width 120 -height 60 -borderwidth 4 and -relief flat-relief flatcanvas with -width 120 -height 60 -borderwidth 4 and -relief raised-relief raisedcanvas with -width 120 -height 60 -borderwidth 4 and -relief sunken-relief sunkencanvas with -width 120 -height 60 -borderwidth 4 and -relief groove-relief groovecanvas with -width 120 -height 60 -borderwidth 4 and -relief ridge-relief ridgecanvas with -width 120 -height 60 -borderwidth 4 and -relief solid-relief solid
Each with -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
1

ztk 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.

canvas with -height 60 and -width 80-width 80canvas with -height 60 and -width 120-width 120
Each with -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 1

ztk 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 1

ztk 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 PIXELS

Returns

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

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1

See also