Take advantage of Tk

GUI toolkit with abundant advantages

By Cameron Laird  1 comment

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 text and canvas widgets; 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.

1 comment

    Anonymous 2 years ago
    Are there any GUI builders (that emit Python code) still in active development for Tk?SpecTcl/guib was promising but seems to be stalled.

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      DevelopmentWhite Papers & Webcasts

      White Paper

      HP NonStop SQL Fundamentals whitepaper

      This whitepaper offers a detailed look into the fundamentals of HP NonStop SQL solutions. See how this system delivers unprecedented levels of application availability with fail-safe data integrity and meets the needs of enterprises with large-scale business critical applications.

      White Paper

      Nebraska Medical Center case study

      See how the Nebraska Medical Center implemented a SQL solution to make information more readily available to streamline operations, improve patient care and facilitate medical research with an enterprise solution running on HP NonStop servers.

      White Paper

      Concepts of NonStop SQL/MX

      For DBAs and developers who are familiar with Oracle solutions and want to learn about NonStop SQL/MX, this whitepaper provides an overview of the similarities and differences between the two products-with a specific focus on implementation.

      White Paper

      6 Things Your CIO Needs to Know About Requirements

      If your organization is not predictably successful on technology projects, there is likely an issue in requirements. CIOs must take action and own requirements maturity improvement. There are 6 main things a CIO must know about requirements.

      Webcast On Demand

      User Experience Monitoring

      In this webinar, you will learn hints & tips for improving end-user response times from Forrester Research analyst, Jean-Pierre Garbani.

      Sponsor: Nimsoft

      See more White Papers | Webcasts

      Ask a question

      Ask a Question