Version 3 of TkCon as an application console

Updated 2011-04-14 22:50:35 by AKgnome

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.