Ruby Programming/GUI Toolkit Modules/Tk

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Of Ruby GUI bindings, the Tk binding is the oldest; it is widely available and still more or less the default toolkit for GUI programming with Ruby. Nevertheless, currently there exists no comprehensive manual for Ruby/Tk; the Ruby book recommends inferring Ruby/Tk usage from the Perl/Tk documentation.

The current Ruby "PickAxe book" has a chapter on Ruby/Tk.

Documentation[edit | edit source]

  • There’s a pretty good tutorial www.tutorialspoint.com/ruby/ruby_tk_guide.htm .
  • There are some pleasantly looking tutorials at tkdocs.com (it’s not really Tk’s official site).
  • After having introducted yourself to the concepts of Tk you should also be able to read TclTk’s official reference (this is also available as man pages on Unix or a CHM file on Windows[1]).

You may also be able to comfortably read PerlTk’s documentation as the Ruby bindings are said to be modeled after Perl’s.

Look and Feel[edit | edit source]

The “look and feel” of your Tk application depends on the version of the Tk library your Ruby interpreter is linked against:

  • Tk 8.4 and below is notorious for its “ugly” look. The widgets have the outdated Motif look and feel, and, on Unix-based systems[2], bitmap fonts are used.
  • Tk 8.5 and above look more modern. It is shipped with the Tile theming engine[3], and widgets now look native to the hosting platform[4]. On Unix-based systems outline fonts are now used.

Availability[edit | edit source]

If the Tk toolkit isn’t already installed on your system, you’ll have to install it. You may use your system’s “package manager” for this.

If you built Ruby before having installed the Tk dev package, it’s likely that it was built without Tk built in. For 1.9 versions you might be able to get away with installing it as a gem but your best bet is to install the tk dev package and reinstall Ruby so that it builds with the Tk bindings.

Windows[edit | edit source]

By default the “old” one click installer has the Tk binaries, however you’ll still need to install the Tk toolkit from ActiveState .

If you’re using the new “rubyinstaller” then for 1.8.6 this might help or for 1.9 this might help

There’s a precompiled gem which should work out-of-the-box: tk-win.
It includes sources and libraries directly from Hidetoshi NAGAI. It’s Ruby 1.9 only.

You could also try the ffi-tk gem, or download this for 1.9 mingw users.

The hope is that future versions of rubyinstaller will come with the binaries built in, then the above work-arounds won’t be necessary.



  1. The ActiveState package for Windows, mentioned above, contains the documentation as a CHM file.
  2. “on X-Window systems”, to be exact.
  3. The Tile theming engine can be installed on older Tk 8.x libraries as well.
  4. Though you may have to “turn on” this feature (?).