Tcl/Tk Cookbook - Using the Canvas


Part-I - Step 1: create a canvas and embed a button widget in it.


frame .mv
wm title . "Animation in canvas"
pack .mv
canvas .mv.c 
pack .mv.c

button .mv.c.but -text Credits -command displayInfo

The script creates a canvas ".mv.c" as the child of a main frame ".mv". It also creates a button ".mv.c.but" with label "Credits" and a -command option to invoke the action "displayInfo".

Embedded Widgets

Some of the item types supported by Tk canvas are rectangle, ellipse, Bezier curve, bitmap and window. Canvas displays window for a widget at a given x,y position and provides the geometry management for the widget. This allows for pre-packaged components with their own built-in functionality to be available alongside general drawing and hyper-linking capabilities of the canvas. (e.g., you may want to embed the BLT-graph widget in a text widget alongside text or in a canvas widget and exploit its built-in capability to provide direct interaction for zooming into specific regions of the graph).

The following Tcl/Tk command appended to the script above, creates a window inside the canvas at x,y position 218,153 for the button ".mv.c.but".


.mv.c create window 218 153 -window .mv.c.but

Note that in this simple example, we could have used a tagged text string and used tag binding to invoke "displayInfo" but the principle of embedding widgets is powerful and appropriate mechanism if you wish to invoke complex ready-made soultions.

If you execute this script you will see: