Next / Previous / Contents

8.13. Canvas rectangle objects

Each rectangle is specified as two points: (x0, y0) is the top left corner, and (x1, y1) is the location of the pixel just outside of the bottom right corner.

For example, the rectangle specified by top left corner (100,100) and bottom right corner (102,102) is a square two pixels by two pixels, including pixel (101,101) but not including (102,102).

Rectangles are drawn in two parts:

  • The outline lies inside the rectangle on its top and left sides, but outside the rectangle on its bottom and right side. The default appearance is a one-pixel-wide black border.

    For example, consider a rectangle with top left corner (10,10) and bottom right corner (11,11). If you request no border (width=0) and green fill (fill='green'), you will get one green pixel at (10,10). However, if you request the same options with a black border (width=1), you will get four black pixels at (10,10), (10,11), (11,10), and (11,11).

  • The fill is the area inside the outline. Its default appearance is transparent.

To create a rectangle object on canvas C:

    id = C.create_rectangle(x0, y0, x1, y1, option, ...)

This constructor returns the object ID of the rectangle on that canvas. Options include:

Table 13. Canvas rectangle options

activedash These options specify the appearance of the rectangle when its state is tk.ACTIVE, that is, when the mouse is on top of the rectangle. For option values, refer to dash, fill, outline, outlinestipple, stipple, and width below.
dash To produce a dashed border around the rectangle, use this option to specify a dash pattern. See Section 5.13, “Dash patterns”.
dashoffset Use this option to start the border's dash pattern at a different point in the cycle; see Section 5.13, “Dash patterns”.
disableddash These options specify the appearance of the rectangle when its state is tk.DISABLED.
fill By default, the interior of a rectangle is empty, and you can get this behavior with fill=''. You can also set the option to a color; see Section 5.3, “Colors”.
offset Use this option to change the offset of the interior stipple pattern. See Section 5.14, “Matching stipple patterns”.
outline The color of the border. Default is outline='black'.
outlineoffset Use this option to adjust the offset of the stipple pattern in the outline; see Section 5.14, “Matching stipple patterns”.
outlinestipple Use this option to produce a stippled outline. The pattern is specified by a bitmap; see Section 5.7, “Bitmaps”.
state By default, rectangles are created in the tk.NORMAL state. The state is tk.ACTIVE when the mouse is over the rectangle. Set this option to tk.DISABLED to gray out the rectangle and make it unresponsive to mouse events.
stipple A bitmap indicating how the interior of the rectangle will be stippled. Default is stipple='', which means a solid color. A typical value would be stipple='gray25'. Has no effect unless the fill has been set to some color. See Section 5.7, “Bitmaps”.
tags If a single string, the rectangle is tagged with that string. Use a tuple of strings to tag the rectangle with multiple tags. See Section 8.4, “Canvas tags”.
width Width of the border. Default is 1 pixel. Use width=0 to make the border invisible. See Section 5.1, “Dimensions”.