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. ---- 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/wish'''M'''.'''N''' "$0" ${1+"$@"} set foo 0 button .b -textvariable foo -command { incr foo } grid .b === [DKF]: Also consider: === #! /usr/bin/env wish'''M'''.'''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... ---- [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] ====== '''-PSE''' [Peter Lewerin]: not very much. Take a look at [DOS BAT magic] for a working solution. ---- !!!!!! %| [Category Tutorial] |% !!!!!!