Tk - Canvas Text Widget



Canvas text widget is used to draw text in canvas. The syntax for canvas text widget is shown below −

canvasName create text x y options

x and y are used to determine the position of text −

Options

The options available for the canvas text widget are listed below in the following table −

Sr.No. Syntax & Description
1

-anchor position

The text will be positioned relative to the x and y locations. Center is default and other options are n, s, e, w, ne, se, sw, and nw.

2

-justify style

Determines the multiline, should be right justified, left justified, or center justified. The default is left.

3

-fill color

The fill color fills the oval with the color.

4

-text text

The text for text widget.

5

-font fontStyle

The font to use for this text.

A simple example for canvas text widget is shown below −

#!/usr/bin/wish

canvas .myCanvas -background red -width 200 -height 200 
pack .myCanvas
.myCanvas create text 100 100 -fill yellow -justify center -text "Hello World.\n
   How are you?" -font {Helvetica -18 bold}

When we run the above program, we will get the following output −

Canvas Text Example
tk_canvas_widgets.htm
Advertisements