C++/Tk
A complete C++ interface to the Tk GUI toolkit
SourceForge.net Logo

Home
Download
Documentation
Examples
People
Events
Links
C++/Tk is an interface to the Tk GUI toolkit, known from many scripting languages like Tcl, Python or Perl.

The C++/Tk library tries to provide not only the Tk functionality, but also its way of writing code, based on the syntax used in Tcl/Tk.

As a result, it is possible to write programming jokes like this
(in fact, the whole C++/Tk library started from this joke):

Tcl/Tk hello world
C++/Tk hello world





proc hello {} {
     puts "Hello Tcl/Tk!"
}





button ".b" -text "Say Hello" -command hello
pack ".b" -padx 20 -pady 6


#include "cpptk.h"
#include <stdio.h>

using namespace Tk;

void hello() {
     puts("Hello C++/Tk!");
}

int main(int, char *argv[])
{
     init(argv[0]);

     button(".b") -text("Say Hello") -command(hello);
     pack(".b") -padx(20) -pady(6);
         
     runEventLoop();
}


Apart from the scaffolding necessary in C++, both programs look almost the same and produce the same result (the actual look and feel depends on the platform, window manager, etc.):

Hello window

If you click the button, the greetings will be printed on the console.

Interesting? See more examples, which include text editor, math animation and the Game of Life!

How does it work?

It works thanks to templates, operator overloading, implicit conversions and temporary objects.
Each C++/Tk expression ends up as equivalent Tcl/Tk command in the string format, which is then fed to the underlying Tcl interpreter.
This means that C++/Tk uses and depends on the Tcl/Tk environment.
It also means that C++/Tk is portable in the same way as Tcl/Tk.

The added values are:
  • good type safety (many errors, which would be run-time errors in Tcl/Tk, are caught at compile-time; and many typos, which would be errors in Tcl/Tk are not errors at all in C++/Tk)
  • very easy integration of the GUI elements with C++ elements like call-back procedures and variables
  • possibility to add simple GUI to C++ programs with just few lines of code, but keeping the whole production cycle with the traditional C++ tools (no additional compiler or preprocessor is needed)
If you think that the above programming joke is funny, feel free to use C++/Tk in your own programs!