next up previous
Next: The result Up: An alternative way: Previous: An alternative way:

An example

# create and pack canvas:
canvas .c
pack .c
# create global images:
image create photo icon1 -file icon1.gif
image create photo icon2 -file icon2.gif
# create image items:
.c create image 50 50 -image icon1 -tags icon1tag
.c create image 100 50 -image icon2 -tags icon2tag
# bind some action to clicking on the items:
.c bind icon1tag <ButtonPress> { puts "click on icon 1" }
.c bind icon2tag <ButtonPress> { puts "click on icon 2" }
# draw some other stuff:
.c create arc 120 50 150 120 -fill green -tags greenthing
.c create line 40 160 160 160 -fill red
.c create oval 50 100 150 120 -outline yellow
.c create rectangle 20 20 180 180 -outline black
.c create text 20 185 -anchor nw -text "Canvas Example, please enjoy"
# bind an action to another item:
.c bind greenthing <Enter> { puts "cursor entered green-thing" }
.c bind greenthing <Leave> { puts "cursor left green-thing" }

First, the canvas is created and put onto the window. Some images are created next. This is done with the image command. The shown statements create two `internal' images with names icon1 and icon2. The image data is loaded from the given files. The names icon1 and icon2 can be used with the -image option of widgets. The image command can do a lot more, see [1] for more information.

The images are used to create a couple of image items on the canvas. The general command to create something on a canvas is
canvas-name create type ...
where type is one of arc, bitmap, image, line, oval, polygon, rectangle, text or window. See [1] for the options that this command can take with each type.

When an item is created on a canvas, a tag-list can be passed. Tags in this list can be used to refer to specific items. An example of this is the binding of some actions to the event clicking on the images. The tag is passed to the bind command determines to which item something will be bound.

The last two parts of the example show some other types of items and another binding.



SE Praktikum
Tue Aug 13 13:33:30 MET DST 1996