Next / Previous / Contents

8.14. Canvas text objects

You can display one or more lines of text on a canvas C by creating a text object:

    id = C.create_text(x, y, option, ...)

This returns the object ID of the text object on canvas C. Options include:

Table 14. Canvas text options

activefill The text color to be used when the text is active, that is, when the mouse is over it. For option values, see fill below.
activestipple The stipple pattern to be used when the text is active. For option values, see stipple below.
anchor The default is anchor=tk.CENTER, meaning that the text is centered vertically and horizontally around position (x, y). See Section 5.5, “Anchors” for possible values. For example, if you specify anchor=tk.SW, the text will be positioned so its lower left corner is at point (x, y).
disabledfill The text color to be used when the text object's state is tk.DISABLED. For option values, see fill below.
disabledstipple The stipple pattern to be used when the text is disabled. For option values, see stipple below.
fill The default text color is black, but you can render it in any color by setting the fill option to that color. See Section 5.3, “Colors”.
font If you don't like the default font, set this option to any font value. See Section 5.4, “Type fonts”.
justify For multi-line textual displays, this option controls how the lines are justified: tk.LEFT (the default), tk.CENTER, or tk.RIGHT.
offset The stipple offset to be used in rendering the text. For more information, see Section 5.14, “Matching stipple patterns”.
state By default, the text item's state is tk.NORMAL. Set this option to tk.DISABLED to make in unresponsive to mouse events, or set it to tk.HIDDEN to make it invisible.
stipple A bitmap indicating how the text will be stippled. Default is stipple='', which means solid. A typical value would be stipple='gray25'. See Section 5.7, “Bitmaps”.
tags If a single string, the text object is tagged with that string. Use a tuple of strings to tag the object with multiple tags. See Section 8.4, “Canvas tags”.
text The text to be displayed in the object, as a string. Use newline characters ('\n') to force line breaks.
width If you don't specify a width option, the text will be set inside a rectangle as long as the longest line. However, you can also set the width option to a dimension, and each line of the text will be broken into shorter lines, if necessary, or even broken within words, to fit within the specified width. See Section 5.1, “Dimensions”.

You can change the text displayed in a text item.

  • To retrieve the text from an item with object ID I on a canvas C, call C.itemcget(I, 'text').

  • To replace the text in an item with object ID I on a canvas C with the text from a string S, call C.itemconfigure(I, text=S).

A number of canvas methods allow you to manipulate text items. See Section 8.6, “Methods on Canvas widgets”, especially dchars, focus, icursor, index, and insert.