Purpose: some gentle introductory examples of Tk
#! /usr/local/bin/wish8.1 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.
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 tkwaitas follows:-
#! /usr/local/bin/wish8.1 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.
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.1 button .b -text 0 -command {.b config -text [expr [.b cget -text]+1]} pack .b ;#RS
The explanation of the second example is this:
JDG: Here's a different approach to the second example---it uses the -textvariable arg:
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
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.3
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\wish83.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