Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


Using Tcl/Tk From C program

How to add an external event loop to Tcl/Tk GUIs embedded into C programs by menczel
Posted: 27 Apr 08


To add a mechanism to the Tk library by which a C program can register a function that will become the main event loop of Tcl/Tk.


Tcl/Tk is very convenient for providing sophisticated GUIs to C programs. 'Guibuilder' makes designing graphical user interfaces in Tcl/Tk very easy. Also Tcl/Tk is truly platform independent: the same script will work under Windows, Linux and Mac OS. For this reason, using a Tcl/Tk GUI for C programs is IMO a viable and attractive alternative to using native C GUI libraries like Gtk.

There are two ways of integrating Tcl/Tk GUIs into C programs. Both have advantages and disadvatages. Actually, one of them simply does not work, I guess that may be considered a disadvantage. winky smile

The standard way is calling 'Tk_Main' from the main module of the C program and registering callbacks for events occuring in the main Tk window. For most applications this is sufficient. However, when you use this method, there seems to be no easy way to regularly call C modules for doing other tasks that must be done independent of the events of the user interface (e.g reading a data stream from the serial port, sending commands or state updates to hardware that your program controls, etc.).

Note: I have seen a comment in one of the docs which indicates that the function 'Tcl_CreateTimerHandler' could be used for this purpose. However, this call creates a one-shot event so the callback would have to set up its timer again and again after each call. Rather clumsy and probably not very efficient.

The Tcl/Tk library in theory provides another C interface method. You can do the creation and initialization of the interpreter and the Tk main window from your C code, then run a main loop in your app. In this case you are free to do any other task in the main loop, as long as you call 'Tcl_DoOneEvent' frequently enough so that events in the Tk main window get serviced. There is example code for this approach in the book 'Practical Programming in Tcl/Tk'.

However, according to my experience this approach does not work. I tested it using both the 8.4 and 8.5 versions of Tcl/Tk under Windows (MinGW development system). My programs crash when they try to create the main Tk window. I traced the problem to a specific Tcl/Tk library function, but have no idea how to fix it.(I could probably do it, but I have no intention to spend days or weeks on understanding, modifying and debugging the window management modules of the Tcl/Tk library.)

In order to solve this problem I have designed a simple method which can be added to the Tk library. We let 'Tk_Main' do all the arcane tasks necessary for starting a functional Tcl/Tk interpreter and creating a main window. Then, when everything already works, we simply hijack the main loop and replace it with our own.

You have to recompile the Tk library if you want to implement this mechanism. The added code does not interfere with other functions in the library, so the new version (theoretically) should be fully compatible with the standard distribution.

Changing the Tk library

Add the following code to 'tk.h' right before the section starting with the statement #include "tkDecls.h" (near the end of the file):


typedef void (* Tk_ExtEventLoop)(void);
#if defined BUILD_tk
  __declspec(dllexport) int Tk_RegisterEventLoop(Tk_ExtEventLoop func);
  __declspec(dllimport) int Tk_RegisterEventLoop(Tk_ExtEventLoop func);

Add the following code to 'tkEvent.c' right before the function 'Tk_MainLoop' (at the end of the file):


static Tk_ExtEventLoop event_loop = NULL;

Tk_RegisterEventLoop(Tk_ExtEventLoop func)
  if (event_loop != NULL || func == NULL)
    return 0;
  event_loop = func;
  return 1;

Modify 'Tk_MainLoop' in 'tkEvent.c' to read as follows:


    if (event_loop != NULL)
      (* event_loop)();        // preempt standard event loop

    while (Tk_GetNumMainWindows() > 0) {

    event_loop = NULL;        // paranoia

Recompile & install the Tk libraries.

The advantage of this mechanism is that only minimal changes are made in the library code, it is platform independent, and timing is under the absolute control of the C application.

How to add and use the external event loop

Following is an example program demonstrating the use of an external main loop.


#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <tcl.h>
#include <tk.h>

static Tcl_Interp *interp = NULL;

static int time_init = 0;

static unsigned long time_base;

static void reset_timer_period(void)

static int init_systime(void)

  if (time_init)
    return 1;
  res = timeBeginPeriod(1);
  if (res != TIMERR_NOERROR)
    return 0;
  time_base = timeGetTime();
  time_init = 1;
  return 1;

static unsigned long get_systime(void)
  if (! time_init)
    return 0;

  return timeGetTime() - time_base;

int tcl_app_init(Tcl_Interp *i)
  interp = i;

     Code must be added here to tell Tcl/Tk where to find
     the necessary libraries and scripts. Removed from
     this example, for clarity.

  if (Tcl_Init(i) == TCL_ERROR)
    return TCL_ERROR;
  if (Tk_Init(i) == TCL_ERROR)
    return TCL_ERROR;
     Code to register callbacks for the GUI must be added
     here. Removed from this example, for clarity.

  return TCL_OK;

#define TCL_CALL_INTERVAL    5
#define OUTPUT_INTERVAL        1000

void event_loop(void)
  unsigned long curr_time, last_tcl_call, last_output;

  last_tcl_call = last_output = curr_time = get_systime();
  while (1)
    curr_time = get_systime();

    // check for time wrap-around, it is unlikely but possible

    if (curr_time < last_tcl_call)
      last_tcl_call = last_output = curr_time;
    else if (curr_time < last_output)
      last_tcl_call = last_output = curr_time;

    // check timeouts and call functions as needed

    if (curr_time - last_tcl_call >= TCL_CALL_INTERVAL)
      last_tcl_call = curr_time;

      if (Tk_GetNumMainWindows() == 0)
    else if (curr_time - last_output >= OUTPUT_INTERVAL)
      last_output = curr_time;
      printf("time = %d\n", (int) curr_time);

int main(int argc, char *argv[])

  if (! Tk_RegisterEventLoop(event_loop))
  Tk_Main(argc, argv, tcl_app_init);

  return 0;

If you wish to see how this works you can download a working copy from my WEB page:

The archive contains the modified version of Tcl/Tk (based on source version 8.5.2) including files needed for program development, as well as a compiled version of the test program shown above. The stuff was compiled using MinGW (a Win32 port of GCC). Instructions for proper installation can be found in the accompanying README.

Back to Tcl/Tk FAQ Index
Back to Tcl/Tk Forum

My Archive


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close