Home Contact

TkDocs

Information you need to build high-quality Tk user interfaces.

This tutorial will quickly get you up and running with the latest Tk from Tcl, Ruby, Perl or Python (coming soon) on Mac, Windows or Linux. It provides all the essentials about core Tk concepts, the various widgets, layout, events and more that you need for your application.

 

Installing Tk

In this chapter, you'll get Tk installed on your machine, verify it works, and then see a quick example of what a Tk program looks like.

Though pretty much all Mac OS X and Linux machines come with Tk installed already, it's usually an older version (typically 8.4.x). You want to make sure you've got at least version 8.5 to use the new widget set, so if that's not already there, you'll want to install the newer version.

Though there are lots of ways to install Tk, the easiest is to download and install one of the versions provided by ActiveState (www.activestate.com).

ActiveState is a company that sells professional developer tools for dynamic languages. They also provide (for free) quality-controlled distributions of some of these languages, and happen to employ a number of core developers of these languages.

 

Installing Tk on Mac OS X

On Mac OS X, the easiest way to get Tk is to install the "ActiveTcl" distribution from ActiveState, which includes Tcl, Tk, plus a number of other extension libraries.

In your web browser, go to www.activestate.com, and follow along the links to download the standard version of ActiveTcl, available as a universal binary. Make sure you're downloading an 8.5.x version, not an older 8.4.x version.

Run the installer to get everything loaded onto your machine. When you're done, you'll find a shiny new application called "Wish 8.5" inside the Utilities folder of your Applications folder. This is the "wish" shell, an application that includes both Tcl and Tk.

If you launch that application, you'll see two windows popup (see below), one titled "Wish" which will contain your application, and the second titled "Console" which is where you can type in Tcl/Tk commands.


The Wish application running on Mac OS X.

For convenient use from the Unix command line, you'll also find a script installed as /usr/local/bin/wish8.5 which will launch the same application.

Installing Tk for Ruby on Mac OS X is one of those "good news, bad news" situations. The good news is that both are actually already installed in the base operating system (in Tiger and Leopard). The bad news is that it's an older version of Tk (8.4.7), whereas we really want an 8.5 version.

To check to see exactly what version of Tk your copy of Ruby is using, from 'irb' run:

require 'tk'
TclTkIp.new._eval "info patchlevel"
In an ideal world, this should return 8.5.0 or newer. But it will probably tell you it's an older version.

The new themed widgets set in Tk 8.5 is derived from a Tk extension called Tile. Tile has been available for Tk 8.4.x, and has been bundled with Ruby's Tk bindings. Despite this, let me emphasize again that you really want a current 8.5.x version of Tk.

RubyTk is a binding that links against an existing but separate Tk library. So, to get the latest version of Tk for Ruby, we're going to have to do two things, first download the latest 8.5.x Tcl/Tk version from ActiveState, and then compile (or re-compile) Ruby to use it.

Install ActiveTcl

The "ActiveTcl" distribution from ActiveState contains the latest Tk, as well as the latest version of Tcl (which Ruby's Tk bindings use internally to talk to Tk). In your web browser, go to www.activestate.com, and follow along the links to download the standard version of ActiveTcl, available as a universal binary. Again, make sure you're downloading an 8.5.x version, not an older 8.4.x version.

Run the installer and everything will be loaded onto your machine.

Recompile Ruby

Make sure you've got Apple's developer tools (i.e. Xcode, which includes gcc and friends). Then if you haven't already got it, go to www.ruby-lang.org to download the latest stable (1.8.x, not 1.9.x) version of Ruby.

Unpack it, and then from the Unix command line, run (note that the "configure" command should all be entered on one line, the <install-dir> should be replaced with the location you'd like your version of Ruby installed:

% ./configure --prefix=<install-dir>
		  --with-tcltk-framework=/Library/Frameworks
		  --enable-pthread
		  --enable-shared
% make && make install

The "/Library/Frameworks" directory is where ActiveTcl installs its libraries, as opposed to "/System/Library/Frameworks" which is used by the version of Tcl/Tk that comes with OS X.

To verify that everything worked, start up your newly compiled copy of 'irb', and type:

% require 'tk'
% TclTkIp.new._eval "info patchlevel"

The first line should load RubyTk; typically if there was a problem with compiling it would show up here. The second line will return the version of Tk that you're running, which should be something like "8.5.2".

Verified steps using ActiveTcl 8.5.2, and Ruby 1.8.7.

PERLTODO

 

Installing Tk on Windows

On Windows, the easiest way to get Tcl/Tk onto your machine is to install the "ActiveTcl" distribution from ActiveState, which includes Tcl, Tk, plus a number of other extension libraries.

In your web browser, go to www.activestate.com, and follow along the links to download the standard version of ActiveTcl for Windows. Make sure you're downloading an 8.5.x version, not an older 8.4.x version.

Run the installer, and follow along. You'll end up with a fresh install of ActiveTcl, usually located in C:\Tcl. From a DOS command prompt, or the Start Menu's "Run..." command, you should then be able to run a Tcl/Tk 8.5 shell via:

% C:\Tcl\bin\wish85

This should pop up a small window titled "wish85", which will contain your application. A second, larger window titled "Console" is where you can type in Tcl/Tk commands. Type "exit" in the console window to exit. You may also want to add C:\Tcl\bin to your PATH environment variable.

RubyTk is a binding that links against an existing but separate Tk library. So, to get the latest version of Tk for Ruby, we're going to have to do two things, first download the latest 8.5.x Tcl/Tk version from ActiveState, and then compile Ruby to use it.

Install ActiveTcl

The "ActiveTcl" distribution from ActiveState contains the latest Tk, as well as the latest version of Tcl (which Ruby's Tk bindings use internally to talk to Tk). In your web browser, go to www.activestate.com, and follow along the links to download the standard version of ActiveTcl for Windows. Again, make sure you're downloading an 8.5.x version, not an older 8.4.x version.

Run the installer and ActiveTcl will be loaded onto your machine, in "C:\Tcl".

Compile Ruby

In this section, we'll describe how to compile Ruby using Visual C++ (usually available as part of Visual Studio).

Yes, it can be done using tools like cygwin, but I still recommend doing it with Visual C++. This shouldn't be an issue even for the rabid "free software" types since they wouldn't touch a Windows machine anyway...

Compiling Ruby so that it finds the correct Tk distribution and so on is incredibly finicky, and depends on environment variables that have to be defined before compiling. If everything isn't just right, it will compile okay, but doing "require 'tk'" will fail.

Install Visual C++

Make sure you have a version of Visual C++ installed on your machine, including the command line tools (e.g. nmake). This can be done for example by installing Visual Studio.

Even fairly old versions (but at least 5.0) of Visual C++ should work. Microsoft does offer an "Express" edition for free; can someone confirm whether this will be sufficient?

Make sure the Visual C++ environment variables are set, and that the Visual C++ tools are in your PATH; this can normally be done by running the "vcvars32.bat" script that is installed as part of Visual C++.

Download Ruby Source

Go to www.ruby-lang.org and download the latest stable (1.8.x, not 1.9.x) source code version of Ruby.

Create a directory that will contain both the source and compiled version, e.g. "C:\ruby". Unpack the source into a subdirectory of that e.g. "C:\ruby\ruby187".

The download will be in the form of a ".tar.gz" or ".tar.bz2" file; Winzip can unpack this for you.

Set Environment Variables

Open a command line, and execute the following DOS commands. Again, this is so that when you compile Ruby it will find the appropriate version of Tcl/Tk on your system. We add these items to the beginning of each environment variable to ensure they are found before anything else that might already by on our paths.

set INCLUDE=c:\tcl\include;%INCLUDE%
set LIB=c:\ruby\lib;c:\tcl\lib;%LIB%
set LIBPATH=c:\ruby\lib;c:\tcl\bin;c:\tcl\lib;%LIBPATH%
set PATH=c:\ruby\bin;c:\tcl\bin;%PATH%
set RUBYPATH=c:\ruby

You and your users will probably want most of these environment variables set to run your program, even after you've compiled Ruby. If you later get errors, particularly when trying to do a "require 'tk'", checking these environment variables is a good first step at troubleshooting.

Compile and Install

In the same command line window, type the following:

cd \ruby\ruby187
win32\configure.bat --prefix=c:\ruby
nmake
nmake DESTDIR=c:\ruby install

To verify that everything worked, start up your newly compiled copy of 'irb' (which would have been installed in "c:\ruby\bin" which is on your PATH), and type:

% require 'tk'
% TclTkIp.new._eval "info patchlevel"

The first line should load RubyTk; typically if there was a problem with compiling it would show up here. The second line will return the version of Tk that you're running, which should be something like "8.5.2".

PERLTODO

 

Installing Tk on Linux

While Linux distributions pretty much all come with Tcl/Tk installed, most include Tk 8.4.x, and we want to make sure to get an 8.5.x version. The easiest way to do this is to install the "ActiveTcl" distribution from ActiveState, which includes Tcl, Tk, plus a number of other extension libraries.

In your web browser, go to www.activestate.com, and follow along the links to download the standard version of ActiveTcl for Linux. Make sure you're downloading an 8.5.x version, not an older 8.4.x version.

Unpack it, and run the installer (install.sh), and follow along. You'll end up with a fresh install of ActiveTcl, located in /opt/ActiveTcl-8.5. You should then be able to run a Tcl/Tk 8.5 shell via:

% /opt/ActiveTcl-8.5/bin/wish8.5

This should pop up a window titled "wish8.5". Type a control-D at the prompt in the terminal window to exit. You may also want to add /opt/ActiveTcl-8.5/bin to your Unix path.

While Linux distributions pretty much all come with Tk installed, and most with Ruby as well, we want to make sure to get the latest version, especially of Tk.

RubyTk is a binding that links against an existing but separate Tk library. So, to get the latest version of Tk for Ruby, we're going to have to do two things, first download the latest 8.5.x Tcl/Tk version from ActiveState, and then compile (or re-compile) Ruby to use it.

Install ActiveTcl

The "ActiveTcl" distribution from ActiveState contains the latest Tk, as well as the latest version of Tcl (which Ruby's Tk bindings use internally to talk to Tk). In your web browser, go to www.activestate.com, and follow along the links to download the standard version of ActiveTcl for Linux. Again, make sure you're downloading an 8.5.x version, not an older 8.4.x version.

Unpack it, and run the installer (install.sh), and follow along. You'll end up with a fresh install of ActiveTcl, located in /opt/ActiveTcl-8.5.

Recompile Ruby

Then if you haven't already got it, go to www.ruby-lang.org to download the latest stable (1.8.x, not 1.9.x) version of Ruby.

Unpack it, and then from the Unix command line, run (note that the "configure" command should all be entered on one line, the <install-dir> should be replaced with the location you'd like your version of Ruby installed, and note also the underscore in "--enable-tcltk_stubs"):

% ./configure --prefix=<install-dir>
	  --with-tcl-dir=/opt/ActiveTcl-8.5
	  --with-tk-dir=/opt/ActiveTcl-8.5
	  --with-tcllib=tclstub8.5 
	  --with-tklib=tkstub8.5
	  --enable-tcltk_stubs
	  --enable-tcl-thread
	  --enable-pthread
% make && make install

To verify that everything worked, start up your newly compiled copy of 'irb', and type:

% require 'tk'
% TclTkIp.new._eval "info patchlevel"

The first line should load RubyTk; typically if there was a problem with compiling it would show up here. The second line will return the version of Tk that you're running, which should be something like "8.5.2".

PERLTODO

 

The Obligatory First Program

To make sure that everything actually did work, let's try to run a "Hello World" program in Tk. While for something this short you could just type it in directly to the interpreter, instead use your favorite text editor to put it in a file.

package require Tk
grid [ttk::button .b -text "Hello World"] 

Save this to a file named 'hello.tcl'. From the wish shell, type:

% source hello.tcl

Couldn't find hello.tcl? You might be looking in the wrong directory. You can either give the full path to hello.tcl, or use Tcl's "pwd" and "cd" commands to see what directory you're in, and change to a different one.

require 'tk'
require 'tkextlib/tile'
root = TkRoot.new() 
button = Tk::Tile::TButton.new(root) {text "Hello World"}.grid
Tk.mainloop()

Save this to a file named 'hello.rb'. Start up 'irb', and from the command prompt, type:

% source "hello.rb"

Couldn't find hello.rb? You might be looking in the wrong directory. You can either give the full path to hello.rb, or use Ruby's "Dir.pwd" and "Dir.chdir" commands to see what directory you're in, and change to a different one.

use Tkx;
Tkx::grid( Tkx::ttk__button(".b", -text => "Hello, world" ) );
Tkx::MainLoop();

Note that there are two underscores between "ttk" and "button".

Save this to a file named 'hello.pl'. From a command prompt, type:

% perl hello.pl

Couldn't find hello.pl? You might be looking in the wrong directory. Try providing the full path to hello.pl.


Our First Program. Some work left to do before the IPO.