Version 40 of Tkcon

Updated 2005-09-12 15:34:16 by rmax

Purpose: to describe the tkcon application and the special features that make it a worth the moments it takes to download.


TkCon provides a console for interacting with Tcl. This console has an input history, the ability to help loading packages, ability to attach to running Tk applications (if on a system which supports Tk's send command),

 What: TkCon
 Where: http://tkcon.sourceforge.net/
 Description: A Tcl/Tk source standard console.  It not only is a replacement
        for the one that comes with Tk on Windows and Macintosh, but can
        also be used under Unix.  Provides command history, path/proc/variable
        name expansion, multiple interpreter consoles, captures stdout and
        stderr, character and proc highlighting, history searching,
        copy/paste between consoles, communicate with other tk
        interpreters, supports dynamically loadable extensions,
        electric character and proc highlighting.  Latest version supports
        attaching to itcl/Tcl 8 namespaces.
        Regular updates occur - check pack on the WWW page for announcements.
        Requires Tcl/Tk 8.0+ .
        Currently version 2.4.
        There is also a megawidget console based on tkcon.
 Updated: 10/2003
 Contact: jeff at hobbs org

TkCon as an application console

You can use TkCon as a console for your applications. This is great for debugging, or giving your users a lot of flexibility with the application. The console window interacts directly with the main interpreter. As of TkCon version 2.4, you can source tkcon.tcl and load it as a quasi-package. (There is no pkgIndex.tcl but see below) Put code like this into your GUI construction.

  #------------------------------------------------------
  #  Tkcon isn't packaged in a library, so we have to
  #  source the code, but it provides a package, and
  #  the commands [tkcon show] and [tkcon hide]
  #------------------------------------------------------
  source tkcon.tcl
  package require tkcon

But this doesn't actually create the console until the first time you use it. You also might find some behavior you don't like. For example, if you close the tkcon console window by clicking the "X", then the application exits. There isn't a good public interface for just creating the console and attaching show/hide to a radio button, so you might end up using code like this, even after the package require tkcon.

  #------------------------------------------------------
  #  The console doesn't exist yet.  If we create it
  #  with show/hide, then it flashes on the screen.
  #  So we cheat, and call tkcon internals to create
  #  the console and customize it to our application.
  #------------------------------------------------------
  set tkcon::PRIV(showOnStartup) 0
  set tkcon::PRIV(root) .console
  set tkcon::PRIV(protocol) {tkcon hide}
  set tkcon::OPT(exec) ""
  tkcon::Init
  tkcon title "My Application Console"

If you have an older version of TkCon (before 2.4), then upgrade. If you can't upgrade, then you can try this code, documented here by FPX. You get the same [tkcon show] and [tkcon hide] commands.

  namespace eval tkcon {}
  set tkcon::PRIV(showOnStartup) 0
  set tkcon::PRIV(root) .tkcon
  set tkcon::OPT(exec) ""
  source tkcon.tcl

PAK: Using the above magic and tkcon 2.4, you can roll your own pkgIndex.tcl:

  package ifneeded tkcon 2.4 [subst {
      namespace eval ::tkcon {}
      set ::tkcon::PRIV(showOnStartup) 0
      set ::tkcon::PRIV(protocol) {tkcon hide}
      set ::tkcon::OPT(exec) ""
      package require Tk
      tclPkgSetup [list $dir] tkcon 2.4 {
          {tkcon.tcl source {tkcon dump idebug observe}}
      }
  }]

Now attaching tkcon to your application is as simple as:

  package require tkcon
  set ::tkcon::PRIV(root) .console  ;# optional---defaults to .tkcon
  pack [button .b -text Console -command { tkcon show }]

tkcon.tcl is not sourced unless you click on the button, which improves application startup speed.


stevel: tkcon packaged as a starkit can be found at http://mini.net/sdarchive/tkcon.kit (corrected 13dec04 AET). The tkcon "Help/Retrieve latest version" option will update the starkit with the latest CVS version of tkcon.tcl.

LV Steve, the code for retrieve latest version would be useful to turn into a proc that became part of tclkit's std environment, so that all starkits might do that. Or at least, all the ones at sf.net...

This application is also part of the ActiveTcl Batteries Included distribution. Tkcon is in the bin directory, which you can find with [info nameofexecutable] and [file dirname]. It is named tkcon.tcl on Windows and tkcon (without .tcl) on Unix.


To get tkcon to work using only the main interpreter and the . toplevel, Jeff Hobbs writes:

You need to pass with -exec "" on the command line (in ::argv) or add the following code before you source tkcon:

  namespace eval ::tkcon {}
  # we want to have only the main interpreter
  set ::tkcon::OPT(exec) ""

Outstanding questions relating to tkcon:


NC Is it possible to use tkcon as the default wish console?


JH: Debugging with tkcon

I was asked:

  • [could someone discuss how to debug with tkcon: explain idebug [L1 ], observe [L2 ], "hot errors", and state introspection.]

idebug is a very simple "debugger" that is really more of an interactive introspection tool. You have to actively add the idebug break lines into your code. You can turn off idebug triggers globally (or by id). You can also add idebug trace calls which just show the call stack anytime they are hit. idebug was inspired by an earlier work done by Stephen Uhler (one of the former Tcl Sunlabs folks).

When you stop at an idebug breakpoint, you can introspect or eval any code you want. There are shortcuts for most introspection, just type '?' to see them.

observe is just a wrapper around trace that allows you to watch variable modifications or command invocations easily in tkcon.

hot errors is the facility in tkcon that makes any error thrown to the toplevel tkcon command line a link. Clicking on the link will pop up the full error (with errorInfo) and, where possible, highlight procs. Green highlighted procs will pop up an edit window with that proc information, while blue highlighted procs will do the same, but highlight the error information it received from the stack trace. In this way, errors in your code that reach the global level become much easier to debug. Try this example in tkcon:

 % proc foo {args} { set a b $args }
 % proc bar {} { foo a b c }
 % bar
 wrong # args: should be "set varName ?newValue?"

and click on the error. Simply by running in tkcon you can do all sorts of introspection and state modification. It has many other features, but now my fingers are tired ...

KPV The way I do the bulk of my debugging with tkcon is by simply pasting in lines of code. I get to a quiescent state in the program, then walk the code by copying lines of my tcl code from the editor and pasting them into the console.

LV most of my programs have much too much state, and interaction, for me to imagine myself being very successful working that way.

KPV Most of my programs proceed in two stages: start up stage and then an event driven stage. To debug the first stage I just put in one or more return to get back to the console prompt, at which I can proceed as described above. The second stage is even easier--you're already at the command prompt.


Victor Wagner has expressed interest in running Tkcon on MS-DOS.


Here is some code for Tkcon remote access over TCP sockets.

See also: tkcon.cfg


Arts and crafts of Tcl-Tk programming


[ Category Application ]