1. Computing
Send to a Friend via Email

Graphical User Interfaces: Installing Tk

Using the Tk Toolkit

By

The Tk GUI toolkit was originally written for the TCL scripting language, but has since been adopted by many other languages including Ruby. Though it's not the most modern of toolkits, it is free and cross-platform and is a good choice for simpler GUI applications. However, before you can begin writing GUI programs, you first have to install the Tk library and the Ruby "bindings." A binding is the Ruby code used to interface with the Tk library itself. Without bindings, a scripting language cannot access native libraries such as Tk.

How you install Tk will vary depending on your operating system.

Installing Tk on Windows

There are numerous ways to install Tk on Windows, but the easiest is to install the ActiveTCL scripting language from Active State. While TCL is a completely different scripting language than Ruby, it's made by the same people who make Tk and the two projects are closely linked. By installing the ActiveState ActiveTCL TCL distribution, you'll also install the Tk toolkit libraries for Ruby to use.

To install ActiveTCL, go to ActiveTCL's download page and download the 8.4 version of the Standard distribution. Though there are other distributions available, none of them have the features you'll need if you only want Tk (and the Standard distribution is also free). Be sure to download the 8.4 version of the download as the Ruby bindings are written for Tk 8.4, not Tk 8.5. However, this may change with future versions of Ruby. Once it's downloaded, double-click the installer and follow the directions to install ActiveTCL and Tk.

If you installed Ruby with the One-Click Installer, then the Ruby Tk bindings are already installed. If you installed Ruby another way and the Tk bindings are not installed, you have two options. The first option is to uninstall your current Ruby interpreter and re-install using the One-Click Installer . The second option is actually far more complicated. It involves installing Visual C++, downloading the Ruby source code and compiling it yourself. Since this is not the normal mode of operation for installing Windows programs, using the One-Click installer is recommended.

Installing Tk on Ubuntu Linux

Installing Tk on Ubuntu Linux is very easy. To install Tk and Ruby's Tk bindings, simply install the libtcltk-ruby package. This will install Tk and Ruby's Tk bindings in addition to any other packages needed to run Tk programs written in Ruby. You can either do this from the graphical package manager, or by running the following command in a terminal.

 $ sudo apt-get install libtcltk-ruby 

Once the libtcltk-ruby package is installed, you will be able to write and run Tk programs in Ruby.

Installing Tk on Other Linux Distributions

Most distributions should have a Tk package for Ruby and a package manager to handle the dependencies. Refer to your distributions' documentation and support forums for more information, but in general you will need either libtk or libtcltk packages as well as any ruby-tk packages for the bindings. Alternatively, you can install TCL/Tk from source and compile Ruby from source with the Tk option enabled. However, since most distributions will provide binary packages for Tk and Ruby Tk bindings, these options should only be used as a last resort.

Installing Tk on OS X

Installing Tk on OS X is much the same as installing Tk on Windows. Download the ActiveTCL version 8.4 TCL/Tk distribution and install it. The Ruby interpreter that comes with OS X should already have Tk bindings, so once Tk is installed you should be able to run Tk programs written in Ruby.

Testing Tk

Once you have Tk and Ruby Tk bindings, it's a good idea to test it out and make sure it works. The following program will create a new window using Tk. When you run it, you should see a new GUI window. If you see any error messages or no GUI window appears, Tk has not been installed successfully.

 #!/usr/bin/env ruby
 require 'tk'
 
 root = TkRoot.new do
   title "Ruby/Tk Test"
 end
 
 Tk.mainloop
 
  1. About.com
  2. Computing
  3. Ruby
  4. Graphical User Interfaces
  5. Tk
  6. Graphical User Interfaces: Installing Tk

©2014 About.com. All rights reserved.