Create a canvas for drawing with the Canvas method.
The Canvas widget uses
a coordinate system with the x coordinate increasing
as you move right, and the y coordinate increasing
as you move down (i.e., the y coordinate is
mathematically upside-down). The x and y coordinates
are specified in pixels by default.
The standard configuration options that apply to$parentwidget->Canvas ( options)
Canvas are:
-background,
-borderwidth,
-cursor,
-height,
-highlightbackground,
-highlightcolor,
-highlightthickness,
-insertbackground,
-insertborderwidth,
-insertofftime,
-insertontime,
-insertwidth,
-relief,
-selectbackground,
-selectborderwidth,
-selectforeground,
-takefocus,
-width,
-xscrollcommand,
and
-yscrollcommand.Other options are:
-closeenough => amountThe distance considered "close enough" to an item to be judged to be within it. Default is 1 pixel.
-confine => booleanWhether to limit the canvas to the scroll region. Default is 1.
-scrollregion => [ x, y, w, h ]Sets the region that the user is allowed to scroll. The option
is a list reference that conveniently corresponds to the return
value of the bbox method.
-xscrollincrement => amount-yscrollincrement => amountTo place graphic elements in a canvas, there are several item creation commands:
createArc Creates an arc contained within the given bounding box. For example, to create an oval bounded by the box from (0,0) to (40,100):
The$canvas->createArc(0,0,40,100, -extent => 360);
-extent option gives a number between 0 and 360 defining
the length of the arc. The default -extent is 90, or 1/4 of
an oval; an extent of 360 gives you a full oval.
The complete list
of options to createArc is:
-extent => degreesCreates an arc of the specified extent. degrees can be any
number between 0 and 360, as described above.
-fill => colorFills the arc with the specified color.
-outline => colorDraws the arc with the specified color (default = black).
-outlinestipple => bitmapDraws the outline with the specified bitmap pattern.
-start => degreesStarts drawing the arc from the specified position, where the position is represented by a number from 0 to 360. The default is 0, which means to start drawing at the 3 o'clock position.
-stipple => bitmapUses the specified bitmap to fill the arc (if -fill
is also specified).
-style => typeDraws the arc as specified. Values are:
'pieslice'Draws lines from the center to the ends of the arc (the default).
'chord'Draws a line connecting the two ends of the arc.
'arc'Draws the arc with no other lines.
-tags => tagnamesAssociates the arc with the specified tag(s). Multiple tag names can be supplied as an anonymous list.
-width => amountThe width of the outline. Default is 1.
createBitmapInserts a bitmap. For example, to place the "calculator" bitmap
at the (0,0) coordinates:
Options are:$canvas -> createBitmap(0, 0, -bitmap => 'calculator');
-anchor => positionAnchors the bitmap at the specified position. Values are "center" (default), "n", "e", "s", "w", "ne", "nw", "se", and "sw".
-background => colorSpecifies the color to use for the "0" pixels in the bitmap (default is to be transparent).
-bitmap => bitmapSpecifies the bitmap name. For a built-in bitmap, just specify the name; for a local bitmap file, specify the name with an "@" symbol preceding it.
-foreground => colorSpecifies the color to use for the "1" pixels in the bitmap (default is black).
-tags => tagnamesAssociates the bitmap with the specified tag(s). Multiple tag names can be supplied as an anonymous list.
createImageCreates an image. For example, to place an image at (0,0):
Options are:$canvas->createImage(0,0, -image => $imgptr);
-anchor => positionAnchors the image at the specified position. Values are "center"
(default), "n", "e", "s", "w", "ne", "nw", "se", and "sw".
-image => $imgptr$imgptr is a pointer to a Photo or Image object
made using a GIF or PPM file. For example:
$imgptr = $mainwindow->Photo(-file => "doggie.gif");
-tags => tagnamesAssociate the image with the specified tag(s). Multiple tag names can be supplied as an anonymous list.
createLine Creates a line or several adjoining lines. For example, to create a line from (0,0) to (100, 100) and then back to (100, 0):
The first four coordinates are required. Any additional coordinates are taken to represent a continuation of that line. Options are:$canvas->createLine (0,0,100,100,100,0);
-arrow => positionSpecifies where to place arrowheads. Values are 'none' (default), 'first', 'last', and 'both'.
-arrowshape => [ head, length, flare ]Specifies the dimensions of the arrow as a three-element anonymous list, describing (in order) the distance from the base to the "head" of the arrow, the distance from the rear point(s) to the head of the arrow, and the distance from the rear point(s) to the line.
-capstyle => typeDefines the type of arrowhead. Values are "butt" (the default), "projecting", and "round".
-fill => colorThe color to use to draw the line.
-joinstyle => typeDefines how multiple lines are joined. Values are "miter" (default),
"bevel", and "round".
-smooth => booleanDetermines whether the lines are drawn with a Bezier spine. Default is 0.
-splinesteps => nDetermines how smooth the Bezier curve is.
-stipple => bitmapDraws the line with the specified bitmap pattern.
-tags => tagnamesAssociates the line with the specified tag(s). Multiple tag names can be supplied as an anonymous list.
-width => amountThe width of the line (default = 1 pixel).
createOvalCreates an oval. For example, to create a circle bounded by the box from (50,50) to (150,150):
Options are:$canvas->createOval(50,50,150,150);
-fill => colorFills the arc with the specified color.
-outline => colorSpecifies the color for the outline (default = black).
-stipple => bitmapSpecifies a bitmap to fill the oval with.
-tags => tagnamesAssociates the oval with the specified tag(s). Multiple tag names can be supplied as an anonymous list.
-width => amountThe width of the outline (default = 1 pixel).
createPolygonCreates a polygon. At least three sets of coordinates are required; the first point is automatically connected to the last point to complete the polygon.
Options are:$canvas -> createPolygon(0,0,130, 20, 90, -35);
-fill => colorThe color to use to fill the polygon.
-outline => colorSpecifies the color for the outline (default = black).
-smooth => booleanDetermines whether the outline is drawn with a Bezier spine. Default is 0.
-splinesteps => nDetermines how smooth the Bezier curve is.
-stipple => bitmapFills the polygon with the specified bitmap pattern.
-tags => tagnamesAssociates the polygon with the specified tag(s). Multiple tag names can be supplied as an anonymous list.
-width => amountThe width of the outline (default = 1 pixel).
createRectangleCreates a rectangle. For example, to create a square with one corner at (0,0) and another at (100,100):
Options are:$canvas->createRectangle(0,0,100,100);
-fill => colorThe color to use to fill the rectangle.
-outline => colorSpecifies the color for the outline (default = black).
-stipple => bitmapFills the rectangle with the specified bitmap pattern.
-tags => tagnamesAssociates the rectangle with the specified tag(s). Multiple tag names can be supplied as an anonymous list.
-width => amountThe width of the outline (default = 1 pixel).
createTextPlaces text in a canvas widget. For example, to write "Broadway" centered at the position (130,-40):
Options are:$canvas->createText(130,-40, -text => "Broadway");
-anchor => positionAnchors the text at the specified position. Values are "center" (default), "n", "e", "s", "w", "ne", "nw", "se", and "sw".
-fill => colorThe color to use for the text.
-font => fontnameThe font for the text.
-justify => positionThe justification of the text (any of 'left', 'right', and 'center'). The default is 'left'.
-stipple => bitmapFills the text with the specified bitmap pattern.
-tags => tagnamesAssociates the text with the specified tag(s). Multiple tag names can be supplied as an anonymous list.
-text => stringSpecifies the text to display.
-width => amountThe maximum length of each line of text. Default is 0, which means that lines are only broken at explicit newline characters.
There is a set of methods for manipulating text items within a Canvas widget. For each of these methods, the first argument is the tag name or tag ID, and subsequent arguments use text indexes as described for the Text widget.
dcharsDeletes characters from a text item, given the tag name or ID, and indexes of the first and last characters to delete.
icursorPlaces the insert cursor at the specified index.
indexGets a numerical index from a named one.
insertAdds a string to the text item.
createWindowEmbeds another widget inside of a canvas. The widget must have been already created as a child of the canvas or of the canvas's parent. Options are:
-anchor => positionAnchors the widget at the specified position. Values are "center" (default), "n", "e", "s", "w", "ne", "nw", "se", and "sw".
-height => amountSpecifies the height of the widget.
-tags => tagnamesAssociates the widget with the specified tag(s). Multiple tag names can be supplied as an anonymous list.
-width => amountThe width of the widget.
-window => $widgetSpecifies the widget to embed.
Each item in a Canvas Widget is given a unique ID when it is created. This ID is
returned from the canvas creation command. In addition, each
item can have a tag associated with it, either when created
or with the addtag method. You can use either the ID
or the tag to refer to an item in the canvas. Unlike IDs,
tags do not have to be unique, which makes it possible to configure
several items as a group.
Two special tags are created automatically. The "all" tag refers to all items in the canvas. The "current" tag refers to the item that the cursor is currently over, if any.
In addition to configure and cget, the following
methods are supported by the Canvas widget.
addtagDefines a tag for an already-created canvas item.
For example, to assign a tag
called "everything" to all items in a canvas:
$canvas->addtag("everything", "all");
To change the tag for an item from "tmp" to "circle":
$canvas->addtag("circle", "withtag", "tmp");
To assign the tag "origin" to the item closest to the coordinates
(0,0):
$canvas->addtag("origin", "closest", 0, 0);
The full list of identifiers is:
aboveAssigns the tag to the item above the specified item in the display list.
allAssigns the tag to all items in the canvas.
belowAssigns the tag to the item below the specified item in the display list.
closestAssigns the tag to the item closest to the specified x,y coordinate.
enclosedAssigns the tag to all items that are completely enclosed within the specified bounding box.
overlappingAssigns the tag to all items that are even partially inside the specified bounding box.
withtagAssigns the tag to all items with the specified tag.
bindBinds a callback to an item. (To bind a callback to the canvas widget itself, you must specify Tk::bind.)
bboxReturns the bounding box of an item. For example, to get the bounding box for all items in the canvas:
$canvas->bbox("all");
itemconfigureConfigures one of the items within the canvas.
Works just like
the configure method for widgets, but the first argument is the
tag name or ID for the canvas item.
itemcgetGets configuration information for one of the items within the canvas.
Works just like
the cget method for widgets, but the first argument is the
tag name or ID for the canvas item.
moveMoves an item on the canvas by adding the specified x and y distances to it.
$canvas->move("circle1", 100, 100);
coordsGets the current x,y coordinates for an item, or moves an item to an explicit x,y coordinate.
lowerSets the priority for the item in the display list to be lower than the item identified by the specified tag or ID.
raiseSets the priority for the item in the display list to be higher than the item identified by the specified tag or ID.
deleteRemoves an item from the canvas. You can specify as many tags or IDs in the argument list as you want.
findFinds the specified items. The first argument can be any of:
aboveFinds the item above the specified item in the display list.
allFinds all items in the canvas.
belowFinds the item below the specified item in the display list.
closestFinds the item closest to the specified x,y coordinate.
enclosedFinds all items that are completely enclosed within the specified bounding box.
overlappingFinds all items that are even partially inside the specified bounding box.
withtagFinds all items with the specified tag.
gettagstypefocuspostscriptRenders the canvas as PostScript. Options are:
-colormap => \@colorcommandSpecifies a PostScript command for setting color values.
-colormode => modeSets the mode to "color" (full color), "gray" (grayscale), or "mono" (black and white).
-file => filenameThe name of the file to store the PostScript output.
-fontmap => \@fontspecSpecifies a font name and point size.
-height => sizeThe height of the area to print.
-pageanchor => positionThe anchor position of the page. Values are "center" (default), "n", "e", "s", and "w".
-pageheight => heightThe height of the printed page.
-pagewidth => widthThe width of the printed page.
-pagex => xThe x positioning point.
-pagey => yThe y positioning point.
-rotate => booleanWhether to rotate to landscape orientation. Default is 0.
-width => sizeThe width of the area to print.
-x => xThe left edge of the canvas.
-y => yThe top edge of the canvas.
scaleChanges the scaling of the canvas or any individual items. For example, to scale the entire canvas to half its dimensions:
$canvas->scale("all", 0, 0, .5, .5);
xviewManipulates the canvas area in view. With no arguments, returns a
list of two numbers between 0 and 1, defining what portion of the
canvas is currently hidden on the left and right sides,
respectively. With arguments, the function of xview
changes:
movetoMoves the specified fraction of the text to the left of the visible portion.
scrollScrolls the canvas left or right by the specified number of units or pages. Used primarily as a callback to a scrollbar; pressing on an arrow would move by units (characters), and pressing on the trough would move by pages. The number is either 1 or -1, to move forwards or backwards, respectively.
yviewManipulates the canvas in view. With no arguments, returns a list of two numbers between 0 and 1, defining what portion of the canvas is currently hidden on the top and bottom, respectively. With arguments, its function changes:
movetoMoves the specified fraction of the canvas area to the top of the visible portion.
scrollScrolls the canvas up or down by the specified number of units or pages. Used primarily as a callback to a scrollbar; pressing on an arrow would move by units (lines), and pressing on the trough would move by pages. The number is either 1 or -1, to move forwards or backwards, respectively.