A User's Guide to Tcl/Tk

Purpose: To discuss and document issues relating to uses of Tcl/Tk (and other Tcl related extension) applications. This is NOT relating to someone writing an application.


Examples of topics that fit here are:

  • How do I determine the default keyboard bindings for Tk widgets?
  • How do I determine what keys are bound?
  • How do I change colors of an application?
  • How do I change the colors of a specific widget in my application?
  • How do I set options for a particular instance of a Tk application?
  • How can I get a Tk application to run from cron?
  • I have a script in a text file - how do I run it?

Feel free to add more topics.


The answers to the first 3 ought to be, in an ideal world, "the same way you find everything else in your desktop environment of choice". To the 4th you would say, "don't do that".


How do I determine the default keyboard bindings for Tk widgets?

Default bindings can be displayed by running bindtags on a widget.

pack [button .b]
puts [bindtags .b]

Which reports:

.b Button . all

To figure out what bindings are in the Button class you can do this:

puts [bind Button]

Which reports:

<ButtonRelease-1> <Button-1> <Leave> <Enter> <Key-space>

Now you can view what the binding does by doing this:

puts [bind Button <Button-1>]

Which reports:

tkButtonDown %W

LV - Seems like a user tool to do the above types of things would be neat - remember, many users not only are not programmers; they are often barely comfortable using the computer. Expecting them to do the above types of things to see what keys do what is probably a bit much.

Having a tool that one can start, then point at a widget and have a little window popping up indicating what keys that widget knows about, would be a user friendly type of tool...

See Tool to browse window bindings.


How do I change colors of an application?

To change colors globally for all Tk applications you can add a .wishrc to your home directory, or if you are in Windows you can create a file and source it to load the custom colors.

My .wishrc looks like this:

tk_setPalette background black foreground white highlightbackground blue activebackground gray70 activeforeground black

For some reason using a .wishrc file doesn't always work correctly.

For example if you start your scripts using the following:

#!/bin/sh
# \
exec wish "$0" "$@"

In this case you should add the following to your script:

catch {source $env(HOME)/.wishrc}

You can also use the resource database to change colors. Would someone more experienced with this care to enlighten the rest of us?

DKF - The option database is both powerful (letting users define appearance schemes for applications, whole desktops, or even just a particular window or widget of an application) and fundamentally annoying (CDE, for example, assumes all the world is Motif or Athena, and installs a set of options that just don't work very well with Tk.) The name database is slightly misleading though, since the search patterns are the keys in the database, and the lookups are performed using unpatterned values (though with a choice of either a widget name or a widget class in each place.) See my option tutorial for more details...

LV - anyone know of a Tk tool written to be a general option query and update type tool? In the early days of Tcl/Tk, John Ousterhout and Brent Welch did an article on Hypertools; the idea of writing Tcl/Tk tools that could query and manipulate other Tcl/Tk tools. In fact, if you read John's original Tk paper [L1 ] you will see hints of this concept even from the beginning.


Why does my application say "Application initialization failed: this isn't a Tk applicationcouldn't connect to display "myserver:0.0""?

The bottom line is that you are likely trying to run an X Window System based Tk application but the $DISPLAY variable is set to a server which doesn't permit you to display the output from that machine. Either change back to the "myserver" machine, or run the appropriate xauth commands to authenticate the machine you are on.

See X server insecure (must use xauth-style authorization) for some tips.


What generally available applications provide users with the hooks to use Tcl to control them? For instance, I believe that GIMP has this ability. The BSD 4.4 updated version of vi - called nvi - does. There is one or more IRC clients which provide this ability. Then there is TiK - the AOL Instant Message client, which can be enhanced by Tcl scripts.

Certainly there are plenty of applications (see http://www.purl.org/NET/Tcl-FAQ/part4.html ) written in Tcl. However, modifying the existing application is quite different than an application designed to allow individual users to create custom functionality.