November 12, 2009, 9:27 PM — Let's look at code:
# This brief script creates a well-behaved button that,
# when pressed, shuts down the host on which it's run.
pack [ttk::button .button -command shutdown -text "Push me to shut down"]
proc shutdown {} {
set now now
if {$::tcl_platform(platform) eq "windows"} {
set now /s
}
exec shutdown $now
}
Yesterday, I lauded Tk as a GUI toolkit which achieves a lot for me. While I'm as comfortable as most to deliver sermons on programming topics, concrete examples are essential for me; let's see what light the few lines above shed on Tk.
To first order, Tk has a lot in common with other toolkits. I can code approximately the same functionality in Swing or Motif or MFC (except for the Unix part) or Qt, and each example will be approximately as meaningful to newcomers to the dialect--that is, you aren't truly going to know how you like a particular toolkit and/or language until you've experienced it for yourself. Part of the reason I recommend Tk is that it takes so little to start: a text like the one above constitutes a meaningful program that you can run after, at most, a few minutes of installation of a Tk shell.
At the same time, there's a lot to explain in just these few commands. The first line specifies a ttk::button, rather than a button. Tile is (approximately) a collection of restyled widgets that I use for most of my work. The new Tile-based ttk::button, on Vista, looks like this
, while the classic button
results from a first line like
pack [button .button -command shutdown -text "Push me to shut down"]
Yesterday's post disappointed and even upset my friend Roy Terry; he tells me I "missed the boat" by not emphasizing Tile, which "has to be the lead and main topic" of any advocacy piece on Tk. Roy has worked with Tk since it was first created, and has plenty of wisdom to pass on to the rest of us. I'm not so much an advocate for Tk, though, as I am for the programmers trying to get jobs done. I write to help them, rather than Tk, although it's probably better for my own income when someone "pumps up" Tk. Tile is certainly an important part of the Tk landscape; its availability is crucial for some developers, particularly those delivering certain styles of applications to Linux platforms. For others, the difference Tile makes is as subtle as the difference between the two snapshots above.
In a few days, I'll follow up with more examples of the elegance Tile provides.
For me, though, much of the Tk story has not changed in the last decade, in that Tk's strengths remain its:
- succinct expressiveness;
- light-weight simplicity and relative uniformity;
- sane default styling;
- powerhouse
textandcanvaswidgets; and - intelligent cross-platform capabilities.
text and canvas are powerful in ways that remain a challenge to describe briefly; they both rely on a "tagging" facility that's essentially unique to Tk. I'll say more about these in a follow-up this weekend.
GUI toolkits are like romantic relationships, in that their charms and frustrations rest on scores of individual details that are hard to explain to others, and nothing at all like what you thought you were after. My favorite parts of Tk now are quite different from what initially caught my eye about it--and, predictably, that early infatuation involved a long, almost farcically-complicated series of accidents. Still, the conclusion remains for you, the reader: Tk deserves a little of your time. Try it for a day. See how it fits your personal style. Let me know what you discover.
Also, please pass back any questions you have about Tk, or other toolkits, for that matter. Do you want to see a demonstration program in a particular language binding? Do you wonder about deployment issues, or licensing? Tell me what you're thinking.















