Version 15 of The Hello World program as implemented in Tcl/Tk

Updated 2009-03-29 21:51:28 by dkf

Purpose: some gentle introductory examples of Tk

#! /usr/local/bin/wish8.5
button .hello -text "Hello, World!" -command { exit }
pack .hello

These two lines place a clickable button on the screen, labeled "Hello, World", and the application terminates when the button is clicked.


23nov03 - TKWAIT!!!

The above example only works if you type it interactively into wish. If you're writing a Tcl/Tk application - that's stored in a script that wish executes - you've got to add a call to tkwait, as follows:-

#! /usr/local/bin/wish8.5
button .hello -text "Hello, World!" -command { exit }
pack .hello
tkwait window .

tkwait starts a main loop — that displays the graphic window containing the "Hello, World!" window you created in the preceding lines — and then waits for the user to click the "Hello, World!" button. Without tkwait, your script exits immediately after the "pack .hello" line. So no window is displayed — and your script appears to do... absolutely nothing! The same applies to the following examples too.

LV I don't understand this comment. I just took the first 3 line program, changed the path to wish to point to my installed wish, and had no need to add a tkwait. However, I would have coded the hello world as:

#! /bin/sh
# \
exec tclsh "$0" "$@"
package require Tk
button .hello -text "Hello, World!" -command { exit }
pack .hello

which reflects the current mind set to a) locate tclsh from the user's $PATH rather than hard coding in a fixed path, and b) loading Tk as a loadable package rather than relying on a specially built interpreter.

Not here. When I insert a "package require Tk" and use tclkit, the above works fine. -jcw

DKF: Indeed, current best practice is to not use tkwait at all unless you have to, even if the script is executed with tclsh. This has been the case since at least 8.4, and was always true for wish, which does something very much like tkwait for you after processing its input script.


Here's a slightly more useful variation: the button displays a counter that is incremented every time the button is clicked:

#! /usr/local/bin/wish8.5
button .b -text 0 -command {.b config -text [expr {[.b cget -text]+1}]}
pack   .b ;#RS

The explanation of the second example is this:

  • Create a button called .b
  • Initialize its text to the value zero
  • When the button is clicked, execute the following command.
    • Change the text of the .b button. The new value is calculated as follows.
      • Get the current value of the button and, using expr, add 1 to that value.
  • The pack statement is what Tk uses to cause a widget to appear on the screen.

JDG: Here's a different approach to the second example — it uses the -textvariable option:

set foo 0
button .b -textvariable foo -command { incr foo }
pack .b

Again, a different approach — useful under different circumstances (I find myself using -textvariable a LOT ... that's why I threw this in). --jdg
RS: You're right, this is better. I considered it too but wanted to write a one-liner — until I saw that I'd need the #! line too... Point taken.


KBK: If you ever encounter a system in which the #! line doesn't work, then try the following (for explanation, see exec magic):

#! /bin/sh
# next line is executed by the shell, but a comment in tcl \
exec /path/to/wishM.N "$0" ${1+"$@"}

set foo 0
button .b -textvariable foo -command { incr foo }
grid .b

DKF: Also consider:

#! /usr/bin/env wish8.5
set foo 0
grid [button .b -textvariable foo -command {incr foo}]

The env program is an ideal helper here, and I like creating and managing widgets on the same line.


If the #! doesn't work, then it is unlikely that the above trick will work either. On the other hand, if #! does work, but the path to the wish is so long that it is causing some heartburn to your shell, then the above mentioned trick works.

However, that seems out of context for this page...


CM: Not really a program, I admit.. but compared to the first script, we can reduce its length significantly by just typing:

$ echo 'pack [button .h -text "Hello, World!" -command exit]' | wish8.5

This assumes, however that you are running Unix. In an MS-DOS shell, something like:

> echo pack [button .h -text "Hello, World!" -command exit] | "C:\Program Files\Tcl\bin\wish85.exe"

should work too. That's 70 bytes to type for a nice Hello World button! And it's easy to understand! (for Perl lovers.. :-).


What does this do under DOS, er windoze?

#\
exec wish $0
pack [button .h -text "Hello, World!" -command exit]

-PSE

Peter Lewerin: not very much. Take a look at DOS BAT magic for a working solution.