Tycho contains two classes that extend the Tk canvas: the IncrCanvas class extends the canvas with complex hierarchical items, while the Slate adds symbolic item manipulation and support for user interaction. IncrCanvas is, as far as possible, fully upwards-compatible with the canvas -- any code that works with the canvas should work if an IncrCanvas is substituted for the canvas. In this tutorial, however, we will always be using a Slate.
IncrCanvas and Slate have (like the Tk canvas) a large number of operations, and this document only gives a cursory overview of most of them. For more detailed information on the kinds of operations supported by the canvas and the Slate, see the Tk canvas documentation and the Slate code documentation.
To illustrate the operation of the Slate, we will create a Slate in
a blank toplevel window. Normally, however, you will use the Slate
inside the Graphics widget, or within your own custom widget. The
::tycho::slate
procedure creates a new Slate. For our
purposes, we will just source a file that
creates the Slate and places it into a top-level Displayer window:
set slate [::tycho::Slate::demoslate]
You may want to experiment further with Slate operations while executing this tutorial. To do so, simply type commands into the Tycho console.
The graphical items drawn on the slate are called just that: items.
The create
method creates new items. Its first argument
is the type of the item; this is followed by two or more coordinates
at which to create the items, and these are followed by zero or more
options. Coordinates are in the form x-value followed by
y-value; options are in the form
-optionname optionvalue
.
The Slate supports all canvas item types, including
rectangle
, oval
, polygon
,
line
, and text
. In addition, the Slate has
complex items composed of one or more of these primitive items,
including Frame
(a pseudo-3D rectangle),
Solid
(a pseudo-3D polygon), labeled ovals and
rectangles, and any other complex item anyone cares to defined with an
Itcl class. (See
Creating custom items.)
Note that primitive item types start with a lower-case
letter; complex item types start with an upper-case letter. Items are
created with the create
method, which returns a unique ID
that can be used later to access the item; here is a selection:
set r [$slate create rectangle 60 20 80 40 -fill red]
set o [$slate create oval 60 60 100 80 -outline blue -width 3]
set l [$slate create line 30 30 30 50 50 50 50 70 -arrow last]
set f [$slate create Frame 120 120 150 140 -borderwidth 4 -color green]
set s [$slate create Solid 100 20 120 20 120 10 140 30 120 50 120 40 100 40]
To read the coordinates of an item, use the coords
method:
$slate coords $r
To change the coordinates of an item, supply arguments to
coords
:
$slate coords $r 50 10 90 25
To move an item, use the move
method:
$slate move $l 10 10
To delete an item, use delete
:
$slate delete $o
Each item has a number of configuration options, which have
names beginning with a dash; the -fill
option to the first
call to create
above is a configuration option. Options
follow the coordinates when an item is created. Later on, options can
be queried with itemcget
:
$slate itemcget $r -fill
itemconfigure
:
$slate itemconfigure $f -relief sunken
itemconfigure
with
no arguments:
$slate itemconfigure $f
$slate find overlapping 100 30 120 60
$slate create rectangle 100 30 120 60 -outline red
$slate addtag fred overlapping 100 30 120 60
$slate addtag fred withtag $f
$slate addtag fred withtag $l
addtag
method has the same kind of specification
as find -- you can add a tag to items enclosed by a
given rectangle, for example.
You can find the "bounding box" of an item or a set of items with a
given tag:
eval $slate create rectangle [$slate bbox fred] -outline grey
delete
and move
:
$slate move fred 10 10
dtag
method removes tags from items:
$slate dtag $f fred
$slate move fred 10 10