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 macOS and Linux machines come with Tk installed already, it's often an ancient version (typically 8.4.x or an early 8.5). You'll want to ensure you have a more recent version (as of this writing, 8.6.17 is the latest in the 8.6 series).

You'll need both Tk and bindings for the language you're using it from. Sometimes these are bundled together, sometimes not. There's generally an easy way and a hard way to install what you need. The easy way involves downloading precompiled binaries or using a package manager. If you can, use the easy way! The hard way involves building Tcl/Tk and the language bindings from source code and is not always for the faint of heart. This tutorial will outline the steps needed to do build from source if that's what you require.

If you were building from source, earlier versions of this tutorial recommended using precompiled Tcl/Tk libraries from ActiveState. As they've moved away from supporting community editions, we no longer recommend this.

Installing Tk on macOS

Install Tk for Python (Tkinter) on macOS

The easy way

Python's binary installers, available at python.org, include a fully-functioning Tkinter.

The hard way: building from source

If you want to build Tkinter from Python source, there are four steps: install developer tools, install Tcl/Tk, build Python, and verify Tkinter.

Install developer tools

To install Apple's developer tools (compiler, etc.), open a Terminal and at the command line, run:

% xcode-select --install

Install Tcl/Tk

Here, you have two good options: using the Homebrew package manager, or compiling Tcl/Tk from source. Do not rely on the version included in macOS (8.5.9, released in 2010) which has several serious bugs easily triggered by Tkinter.

Homebrew. If you already use Homebrew, you can install Tcl/Tk using the following command:

% brew install tcl-tk

Compiling Tcl/Tk from source. Visit www.tcl.tk and download the source code for both Tcl (e.g., tcl8.6.17-src.tar.gz) and Tk (e.g., tk8.6.17-src.tar.gz). Create a directory on your system and unpack them.

Open a terminal, change into the directory where you unpacked the Tcl and Tk source, and run the following commands. Adjust the version numbers (e.g., 8.6.17) as needed. This will build Tcl and Tk and install them in bin, lib, and include directories next to the source code; change the prefix option if you want to install them elsewhere.

% export TCLTKDIR=`pwd`
% cd tcl8.6.17/unix
% ./configure --with-system-libtommath --prefix=\$TCLTKDIR
% make && make install
% cd ../../tk8.6.17/unix
% ./configure --enable-aqua=yes --without-x --with-tcl=\$TCLTKDIR/lib
% make && make install

As of this writing, the build instructions (in macosx/README in the Tcl/Tk source) are woefully out of date and are best ignored. Of note, along with building a Unix-style command line binary and library, Tcl/Tk can also be built as a macOS framework.

Compile Python

Instructions on obtaining and compiling Python can be found at devguide.python.org. The only trick is telling it where to find the versions of Tcl/Tk you installed. Python's build system goes to great lengths to try to figure this out, and may even get it right. But it may also find a different (older) version on your system. To avoid any unexpected surprises, you can set two environment variables before building to tell it exactly which Tcl/Tk installation to use.

% export TCLTK_CFLAGS="-I\$TCLTKDIR/include"
% export TCLTK_LIBS="\$TCLTKDIR/lib/libtcl8.6.dylib \$TCLTKDIR/lib/libtk8.6.dylib"
% ./configure --with-pydebug && make -j8

Verify Tkinter

Test out what you've built. Start Python from your terminal, e.g.,

% ./python.exe

From the Python command prompt, enter these two commands:

>>> import tkinter
>>> tkinter._test()

This should open a window saying, e.g., "This is Tcl/Tk version 8.6.17."

Install Tk for Tcl on macOS

The easy way

If you already use the Homebrew package manager, you can install Tcl/Tk using the following command:

% brew install tcl-tk

The hard way: building from source

To install Tcl/Tk from source, there are three steps: install developer tools, install Tcl/Tk, and verify.

Install developer tools

To install Apple's developer tools (compiler, etc.), open a Terminal and at the command line, run:

% xcode-select --install

Install Tcl/Tk

Visit www.tcl.tk and download the source code for both Tcl (e.g., tcl8.6.17-src.tar.gz) and Tk (e.g., tk8.6.17-src.tar.gz). Create a directory on your system and unpack them.

Open a terminal, change into the directory where you unpacked the Tcl and Tk source, and run the following commands. Adjust the version numbers (e.g., 8.6.17) as needed. This will build Tcl and Tk and install them in bin, lib, and include directories next to the source code; change the prefix option if you want to install them elsewhere.

% export TCLTKDIR=`pwd`
% cd tcl8.6.17/unix
% ./configure --with-system-libtommath --prefix=\$TCLTKDIR
% make && make install
% cd ../../tk8.6.17/unix
% ./configure --enable-aqua=yes --without-x --with-tcl=\$TCLTKDIR/lib
% make && make install

As of this writing, the build instructions (in macosx/README in the Tcl/Tk source) are woefully out of date and are best ignored. Of note, along with building a Unix-style command line binary and library, Tcl/Tk can also be built as a macOS framework.

Verify

Again from the Unix command line, run the wish8.6 binary (installed in the bin) directory. It should popup a small window. Back at the command line, enter the following Tcl command which should return the correct version, e.g., 8.6.17.

% info patchlevel

Install Tk for Ruby (Ruby/Tk) on macOS

Sep/2025: This section of the tutorial has not yet been updated.

While previous versions of macOS included both Ruby and Tk (albeit older 8.4 versions), since Snow Leopard this has no longer been the case.

Ruby/Tk 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 first download the latest 8.6.x Tcl/Tk version from ActiveState.

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 Community Edition of ActiveTcl. Note that you will need to create an account with ActiveState (no cost) to download it. Again, make sure you're downloading an 8.6.x version.

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

Install Ruby

Next, you'll want to install Ruby. There are multiple ways to do this, as explained at www.ruby-lang.org. One option is to use a package manager like Homebrew. Once it's been installed (at /usr/local/bin/brew) you can install Ruby from a command prompt (e.g. Terminal) via:

% brew install ruby

The initial "%" is the Unix shell prompt; you don't have to type it.

That should put the Ruby binaries in /usr/local/opt/ruby/bin). You can check this via brew info ruby. Below, where we use gem or irb, make sure you're running the version you just installed. One way to do that is specifying the full path.

Install Ruby/Tk

Next, you'll need to download and install Ruby's Tk module, which is packaged as a Ruby gem. To do so, from the command prompt, run:

% /usr/local/opt/ruby/bin/gem install tk

The Tk gem will look for installed versions of Tcl and Tk in /Library/Frameworks, which is where ActiveTcl puts them.

To verify that everything worked, start up /usr/local/opt/ruby/bin/irb and type:

% require 'tk'
% Tk::TK_PATCHLEVEL

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

Install Tk for Perl (Tkx) on macOS

Sep/2025: This section of the tutorial has not yet been updated.

For modern Tk programming using Perl, the "Tkx" module is highly recommended, and we'll be using that here. It links against an existing but separate Tk library. So, to get the latest version of Tk for Perl, we're going to have to do first download the latest 8.6.x Tcl/Tk version from ActiveState.

This tutorial used to rely on the ActivePerl distribution from ActiveState, which bundled a full Tcl/Tk installation, as well as the Tkx module. Unfortunately, as of this writing, there is not a macOS version of ActivePerl available.

Install ActiveTcl

The "ActiveTcl" distribution from ActiveState contains the latest Tk, as well as the latest version of Tcl (which Perl'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 Community Edition of ActiveTcl. Note that you will need to create an account with ActiveState (no cost) to download it. Again, make sure you're downloading an 8.6.x version.

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

Install Perl

Next, you'll want to install Perl. There are multiple ways to do this, as explained at www.perl.org. One option is to use a package manager like Homebrew. Once it's been installed (at /usr/local/bin/brew) you can install Ruby from a command prompt (e.g. Terminal) via:

% brew install perl

The initial "%" is the Unix shell prompt; you don't have to type it.

That should put the Perl binaries in /usr/local/opt/perl/bin). You can check this via brew info perl. Below, where we use perl, make sure you're running the version you just installed. One way to do that is specifying the full path.

Install Tkx

Next, you'll need to download and install Perl's Tkx module. We can grab it from CPAN. Unfortunately, at present it will not install correctly due to errors in its tests. We can bypass the tests and install it anyway. To do so, from the command prompt, run:

% /usr/local/opt/perl/bin/perl -MCPAN -e "CPAN::Shell->notest('install','Tkx')"

The Tkx module will look for installed versions of Tcl and Tk in /Library/Frameworks, which is where ActiveTcl puts them.

To check that this worked, run this from the Unix command line:

% /usr/local/opt/perl/bin/perl -MTkx -e 'print Tkx::info("patchlevel");'

This will return the version of Tcl/Tk that it found. It should be something like "8.6.14".

Installing Tk on Windows

Install Tk for Python (Tkinter) on Windows

The easy way

Python's binary installers, available at python.org, include a fully-functioning Tkinter.

The (not so) hard way: building from source

Actually, if you're able to build Python already on Windows, you've very likely already built Tkinter. As part of the build process, the necessary Tcl/Tk source code is automatically downloaded and compiled for you. So not so hard, is it?

Once you've compiled Python, test it to ensure that Tkinter works. Start Python, and from its command prompt, enter these two commands:

>>> import tkinter
>>> tkinter._test()

This should open a window saying, e.g., "This is Tcl/Tk version 8.6.15."

Install Tk for Tcl on Windows

Sep/2025: This section of the tutorial has not yet been updated.

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 Community Edition of ActiveTcl for Windows. Make sure you're downloading an 8.6.x version. Note that you will need to create an account with ActiveState (no cost) to download it.

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

% C:\ActiveTcl\bin\wish

This should pop up a small window titled "wish", which will contain your application. A second, larger window titled "Console" is where you can type in Tcl/Tk commands. To verify the exact version of Tcl/Tk that you are running, type the following:

% info patchlevel

We want this to be returning something like '8.6.14'.

Type "exit" in the console window to exit. You may also want to add C:\ActiveTcl\bin to your PATH environment variable.

Install Tk for Ruby (Ruby/Tk) on Windows

Sep/2025: This section of the tutorial has not yet been updated.

Ruby/Tk is the binding for Tk. In the distant past, installing it on your Windows machine used to be pure hell, involving installing a separate version of Tcl/Tk, downloading a development environment like Visual Studio, downloading the Ruby source code, carefully compiling Ruby, ...

Luckily, it is now only mildly painful, thanks to the good folks behind RubyInstaller for Windows.

The one-click installer used to include everything you needed to run Ruby/Tk, including the underlying Tcl/Tk libraries. Unfortunately, Tk was removed from the Ruby standard library (stdlib) in version 2.4, and made available as an external gem. RubyInstaller followed suit.

Install ActiveTcl

First, you'll need to install Tcl/Tk.

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 Community Edition of ActiveTcl for Windows. Make sure you're downloading an 8.6.x version. Note that you will need to create an account with ActiveState (no cost) to download it.

Run the installer, and follow along. You'll end up with a fresh install of ActiveTcl in C:\ActiveTcl.

Install Ruby

Next, go to rubyinstaller.org. Download and run the installer, which will install everything into the directory you choose, e.g. C:\Ruby26.

Install Ruby/Tk

Next, you'll need to download and install Ruby's Tk module, which is packaged as a Ruby gem. To do so, open a command prompt and run:

% gem install tk

The initial "%" is the Unix shell prompt; you don't have to type it.

Tell Ruby where to find ActiveTcl

You're not done yet. If you try to use Tk from Ruby, it will complain that it can't find the underlying Tcl/Tk libraries. We'll need to do a couple of things to fix that.

First, Ruby needs to find the tcl86t.dll and tk86t.dll shared libraries. These are located in C:\ActiveTcl\bin. Make a copy of them somewhere Ruby can find them, e.g. C:\Ruby26\bin.

Second, the Tcl and Tk shared libraries will look for a bunch of initialization and other scripts which were installed as part of ActiveTcl. The best way to specify where to find them is to set the TCL_LIBRARY and TK_LIBRARY system environment variables.

This can be done in the Windows control panel (or search for "system environment variables" from the taskbar). In Windows 10, you'll find a button labelled "Environment Variables..." in the "Advanced" tab of "System Properties". Add these system variables:

TCL_LIBRARY     C:\ActiveTcl\lib\tcl8.6
TK_LIBRARY      C:\ActiveTcl\lib\tk8.6

If you're running a shell via command prompt you'll need to restart it to see those new additions.

To verify the version of Tk, start up your newly installed copy of 'irb' (which would have been installed in C:\Ruby26\bin), and type:

% require 'tk'
% Tk::TK_PATCHLEVEL

The first line should load Ruby/Tk. The second line will return the version of Tk that you're running, which should be something like "8.6.14".

Install Tk for Perl (Tkx) on Windows

Sep/2025: This section of the tutorial has not yet been updated.

For modern Tk programming using Perl, the "Tkx" module is highly recommended, and we'll be using that here. The easiest way to get set up is to use the "ActivePerl" distribution from www.activestate.com.

The "ActivePerl" distribution from ActiveState includes not only Perl, but also recent versions of Tk and Tcl (which Tkx uses internally to talk to Tk). In your web browser, go to www.activestate.com, and follow along the links to download the Community Edition of ActivePerl. Note that you will need to create an account with ActiveState (no cost) to download it.

Run the installer and everything will be loaded onto your machine. On our machine, perl.exe was installed at "C:\Perl64\bin"

To find out what version of Tk Perl and Tkx are using, run this from the Windows command prompt:

% perl -MTkx -e "print Tkx::info("patchlevel");"

We want this to be returning something like "8.6.14".

Versions of ActivePerl prior to 5.10 (and some of the first 5.10 builds) included earlier versions of Tcl/Tk (8.4.x rather than 8.5.x). Use a more recent version, and and verify that you do have Tk 8.5 or newer.

Installing Tk on Linux

Install Tk for Python (Tkinter) on Linux/X11

The easy way

Most Linux distributions install a recent version of Python by default, or it can be installed using their package manager. Some Linux distributions separate out Tkinter support into a separate package that isn't automatically installed. If so, they'll usually notify you how to proceed when you try to import tkinter. On many versions of Ubuntu for example, you need to install a package named python3-tk.

To verify Tkinter is installed correctly, start Python, and from its command prompt, enter these two commands:

>>> import tkinter
>>> tkinter._test()

This should open a window saying, e.g., "This is Tcl/Tk version 8.6.16."

The hard way: building from source

If you want to build Tkinter from Python source, there are four steps: install developer tools, install Tcl/Tk, build Python, and verify Tkinter.

Install developer tools and libraries

If needed, use your package manager to install standard development tools needed to build Tcl and Python: gcc, make, git, etc., including the X11 headers and libraries, which are often in a package named something like libx11-dev.

Install Tcl/Tk

Again, you may wish to install Tcl/Tk using your package manager. But if not, visit www.tcl.tk and download the source code for both Tcl (e.g., tcl8.6.17-src.tar.gz) and Tk (e.g., tk8.6.17-src.tar.gz). Create a directory on your system and unpack them.

Open a terminal, change into the directory where you unpacked the Tcl and Tk source, and run the following commands. Adjust the version numbers (e.g., 8.6.17) as needed. This will build Tcl and Tk and install them in bin, lib, and include directories next to the source code; change the prefix option if you want to install them elsewhere.

% export TCLTKDIR=`pwd`
% cd tcl8.6.17/unix
% ./configure --prefix=\$TCLTKDIR
% make && make install
% cd ../../tk8.6.17/unix
% ./configure --with-tcl=\$TCLTKDIR/lib
% make && make install

Compile Python

Instructions on obtaining and compiling Python can be found at devguide.python.org. The only trick is telling it where to find the versions of Tcl/Tk you installed. Python's build system goes to some lengths to try to figure this out. But it may also find a different (older) version on your system. To avoid any unexpected surprises, you can set two environment variables before building to tell it exactly which Tcl/Tk installation to use.

% export TCLTK_CFLAGS="-I\$TCLTKDIR/include"
% export TCLTK_LIBS="\$TCLTKDIR/lib/libtcl8.6.dylib \$TCLTKDIR/lib/libtk8.6.dylib"
% ./configure --with-pydebug && make -j8

Verify Tkinter

When everything is built, be sure to test it out. Start your newly-built Python from your terminal, and from the Python command prompt, enter these two commands:

>>> import tkinter
>>> tkinter._test()

This should open a window saying, e.g., "This is Tcl/Tk version 8.6.17."

Install Tk for Tcl on Linux/X11

The easy way

Most Linux distributions include a recent version of Tcl/Tk, or it can be installed using their package manager. Look for a package named something like tk8.6.

To verify, from the Unix command line, run the wish8.6 binary (installed in /usr/bin or similar). It should popup a small window. Back at the command line, enter the following Tcl command which should return the correct version, e.g., 8.6.17.

% info patchlevel

The hard way: building from source

If you want to build Tcl/Tk from source, there are three steps: install developer tools, install Tcl/Tk, and verify.

Install developer tools and libraries

If needed, use your package manager to install standard development tools needed to build Tcl and Python: gcc, make, git, etc., including the X11 headers and libraries, which are often in a package named something like libx11-dev.

Install Tcl/Tk

Visit www.tcl.tk and download the source code for both Tcl (e.g., tcl8.6.17-src.tar.gz) and Tk (e.g., tk8.6.17-src.tar.gz). Create a directory on your system and unpack them.

Open a terminal, change into the directory where you unpacked the Tcl and Tk source, and run the following commands. Adjust the version numbers (e.g., 8.6.17) as needed. This will build Tcl and Tk and install them in bin, lib, and include directories next to the source code; change the prefix option if you want to install them elsewhere.

% export TCLTKDIR=`pwd`
% cd tcl8.6.17/unix
% ./configure --prefix=\$TCLTKDIR
% make && make install
% cd ../../tk8.6.17/unix
% ./configure --with-tcl=\$TCLTKDIR/lib
% make && make install

Verify

Again from the Unix command line, run the wish8.6 binary (installed in bin directory). It should popup a small window. Back at the command line, enter the following Tcl command which should return the correct version, e.g., 8.6.17.

% info patchlevel

Install Tk for Ruby (Ruby/Tk) on Linux/X11

Sep/2025: This section of the tutorial has not yet been updated.

To get Ruby/Tk working on Linux, we'll rely on your distribution's package manager. The package names and commands shown here are for Ubuntu, and may be different on your system.

Because Ruby/Tk is an add-on gem, it needs to be compiled on your system. That means we're going to need to install the development versions of the Tcl/Tk libraries (tk8.6-dev) as well as Ruby (ruby2.7-dev plus ruby2.7 for the command-line tools like irb and gem]). This will also ensure we have the necessary compilers, dependent libraries like X11, and so on.

% sudo apt install tk8.6-dev ruby2.7 ruby2.7-dev

The initial "%" is the Unix shell prompt; you don't have to type it.

Finally, you can install the Ruby/Tk binding with:

% sudo gem install tk

To verify that everything worked, start up irb and type:

% require 'tk'
% Tk::TK_PATCHLEVEL

The first line should load Ruby/Tk; 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.6.14".

Install Tk for Perl (Tkx) on Linux/X11

Sep/2025: This section of the tutorial has not yet been updated.

For modern Tk programming using Perl, the "Tkx" module is highly recommended, and we'll be using that here. The easiest way to get set up is to use the "ActivePerl" distribution from www.activestate.com.

The "ActivePerl" distribution from ActiveState includes not only Perl, but also recent versions of Tk and Tcl (which Tkx uses internally to talk to Tk). In your web browser, go to www.activestate.com, and follow along the links to download the Community Edition of ActivePerl. Note that you will need to create an account with ActiveState (no cost) to download it.

Run the installer and everything will be loaded onto your machine, in e.g. /opt/ActivePerl-5.28.

To find out what version of Tk Perl and Tkx are using, run this from the command line:

% perl -MTkx -e 'print Tkx::info("patchlevel");'

We want this to be returning something like "8.6.14".

Versions of ActivePerl prior to 5.10 (and some of the first 5.10 builds) included earlier versions of Tcl/Tk (8.4.x rather than 8.5.x). Use a more recent version, and and verify that you do have Tk 8.5 or newer.

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.

from tkinter import *
from tkinter import ttk
root = Tk()
ttk.Button(root, text="Hello World").grid()
root.mainloop()

Save this to a file named "hello.py". From a command prompt, type:

% python hello.py

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

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 [tk::inl "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.

Not working? Are you sure you're using an 8.5 or newer version of Tcl/Tk? See the install chapter...

screen shot
Our first program. Some work left to do before the IPO.