Updated 2018-09-28 13:32:51 by PeterLewerin

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.

Incrementing a counter

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.
  • The window is deleted by using the window manager's normal close functions (often an X in the corner); this is Tk's default behavior.

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 wishM.N
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...

One Liners edit

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.. :-).

DKF: If your PATH environment variable is set right, that last one can be shortened to:
> echo pack [button .h -text "Hello, World!" -command exit] | wish85

What does this do under DOS, er windoze?
exec wish $0
pack [button .h -text "Hello, World!" -command exit]


Not very much. Take a look at DOS BAT magic for a working solution.

Absolute beginner  edit

[Robbie] Could anyone tell me how to write a very simple tcl-tk script that would simply say: Hello world.

I heard a programmer named JC WIippler wrote an excellent interpreter or should I call it a script compiler. Please tell me the name of the app and tell me where I could d'load if from.

Tx for the help.

gkubu 1) see The Hello World program as implemented in Tcl/Tk 2) I guess you are talking of Starpack. See also sdx and How to create my first Starpack

[Robbie] Tx [gbuku]. I was talking about an app programmed by [Jc Wippler] to interpret. I'll check his homepage. I understand the procedure to write a tcl-TK script I the following:

  • a) I type the script into an editor like Notepad (not Word)
  • b) It will save with the extension .txt. I may put the tcl extension (.tcl)
  • c) I have to run the file in an interpreter to determine if my code is right or wrong

If it is right, the file will generate an exe file; If there are errors, it won't. In such a case I<d have to correct the errors. Am I right?

Tx again!

gkubu You don't create an exe file, the script is always interpreted (but the interpreter may create bytecode on the fly). For an interactive use, start a wish console. Two windows will appear, a wish console and a toplevel window. Enter into the console as suggested (% is the prompt sign)
 %button .hello -text "Hello, World!" -command { exit }
 %pack .hello

You will see the button appear in the toplevel window.

You can save the two lines in a text file (e.g. with extension .tcl), and open/start the file with a wish application. To facilitate this, you may link the .tcl extension to the wish application.

Should you want a standalone application, you may create a starpack with an .exe extension. This is useful if you want to transfer your program to a machine without tcl/tk interpreter. However, this is a somewhat advanced topic. You don't need it to learn tcl. Perhaps Online Tcl and Tk Tutorials is more useful for beginners.

[Robbie] Fiele danke Gerhart, I'll try all that. You are indeed a very promising beginner!

See also Show me an example