Tcl/Tk Cookbook - Using the Canvas


Purpose

This chapter introduces the Tk canvas widget which can be used to display objects made of one or more drawing primitives. It introduces some of the special features of Tk canvas widget such as embedded widgets and tagging names to displayed objects so that specific behaviour can be associated with them (e.g., selecting a region in a map invokes a video to play a quick scenic tour of the region).

Tk canvas supports additional features such as Postscript output, scrolling, searching (objects with a given property or closest to a given point), editable text and device indepentent coordinates (also supports pixel based dimension specification).

You will need

At this point it is assumed that the reader has read through the previous chapters. Scripts for creating buttons, scrollbars etc. are made use of in this section but are described in detail in the previous chapter.

Dish to Serve Up

Part-I:

Create a simple canvas based application to animate a text string when a button is pressed.

Part-II:

Create an application to display Western Europe. When the user clicks the left mouse button over a country, its name should appear in an entry widget. Alternatively, when the user enters a country name, ring a note and flash the region of the country.

Recipe

The script for both parts (mo.tcl and eu.tcl) are in the Cookbook's code directory, under ch4 subdirectory.

Part-I

Place the script for all three steps of Part-I in animate.tcl. The script is in ~cookbook/code/ch4/mo.tcl.

  1. create a canvas and embed a button widget in it.
  2. write a procedure to animate (move) a tagged object in the canvas.
  3. create an object for animation and control the animation by reconfiguring the button widget.

Part-II

ssumed that you will place the script for all the three of the following into a single Tcl script file called EU.tcl. The script is in the file eu.tcl in the Cookbook code diectory under ch4.

  1. create a canvas to display Western European map data, an entry widget for the user to input a country name, buttons to clear the canvas and exit the application.
  2. display the data and tag the country names with bindings for displaying the name of the country when the user clicks within its borders.
  3. create procedures to flash the country for which the name is specified.