Basics of Tk

If you would like to type along with this portion of the basic tutorials, you should stop your tclsh (with control D), and use the Tk interpretive shell. Type 'blt_wish' to start a Tk interactive shell going and you will be presented with a '%' prompt, and a small window should appear on your screen. This shell is just like the tclsh, but it has graphics capabilities.

Using Tk, you use Tcl scripts just like before, but now you have more fun commands to play with.

Tk uses X to implement a ready-made set of controls with a motif like look and feel. These are called widgets. Each widget is a member of a class in a hierarchy of other widgets. Each widget can contain any number of children widgets. Each widget has a name, which represents the widget, as well as where it is in the widget tree.


.a.b.c

This name refers to the widget 'c' which is a child of b, which in turn is a child of a, which is a child of the main widget (.).
Some of the different widget types are: labels, buttons, checkbuttons, radiobuttons, canvases, messages, listboxes, scrollbars, scales, entries, frames, menus and menubuttons.
To create a widget you call the widget class you want, give it a name, and whatever configuration options you desire

label .lab -text "Hello World"

You will not actually see the widget until you use Tk's geometry manager to place it somewhere. There are a few kinds of geometry management in Tk, but they are too complex to go into great detail here. For now, to make your widget appear on the screen, you must pack it.

pack .lab



Alright already, let's create our first Tk application