Basic Concepts

8.1  Introduction

Tk (short for “toolkit”) is the most widely used GUI toolkit for Tcl. There are several others, like Gnocl, based on Gtk, but in this tutorial we will only look at Tk.

Here is a simple program to illustrate the style of programming:

   #
   # For generality: load Tk
   #

   package require Tk

   #
   # Define the callback procedure used by the pushbutton
   #
   proc handleMsg {} {
       tk_messageBox -title Message -message $::msg -type ok
       set ::msg ""
   }

   #
   # Create the widgets
   #

   label  .label -text "Enter text:"
   entry  .entry -textvariable msg
   button .button -text Run -command handleMsg

   #
   # Make the widgets visible
   #

   grid .label  .entry -sticky news
   grid .button -

   #
   # We want to be able to resize the entry ...
   #
   grid columnconfigure . 1 -weight 1

This program, when run in a Tk-enabled shell like wish, will show the window shown in Figure 1 (with details depending on the operating system and windowing environment).

Figure 1:  Example Tk GUI.

In fact, this program will run in the command-line shell tclsh as well, thanks to the package require command.

You can fill in text in the text entry box at the right. If you press the pushbutton, a message appears and the entry box is cleared. You can resize it and only the entry box will become wider or narrower.

With just a few lines of code we have created a program that has a surprising amount of functionality. Even though it is small and does not do much of any use, the code does show many aspects you will find in actual GUI programs:

Here are a few more aspects not shown in this simple example, but they will be discussed later on:

One very important aspect that is very much hidden in the wish shell is this: when the shell reaches the end of the script, it does not stop, but instead starts an event loop. Only when the event loop is running is the GUI alive: then it will display the windows, respond to actions from the user and so on.

As Tk is at least as dynamic as Tcl itself, you can easily experiment with widgets in an interactive shell. Just start wish and try commands like:

   text .t
   pack .t

These two commands bring up what is essentially a complete text editor!