Updated 2018-01-29 21:09:24 by DPG

Summary  edit

tkcon is a replacement for the standard console that comes with Tk (on Windows/Mac, but also works on Unix). The console itself provides many more features than the standard console. tkcon works on all platforms where Tcl/Tk is available. It is meant primarily to aid one when working with the little details inside tcl and tk, giving Unix users the GUI console provided by default in the Mac and Windows Tk. It's also not a bad replacement for the default MS-DOS shell (although it needs lots of fine tuning).

Attributes  edit

website
http://tkcon.sourceforge.net/
current release
2009-02-26
contact
jeff at hobbs org

More Up To Date Version for Tcl/Tk 8.6
website
https://github.com/wjoye/tkcon
last commit
2017-01-27

Documentation  edit

official reference
tkcon.cfg
Pasting commands into Tkcon
Using TkCon as an application console
Using Tkcon as an IDE shell
Tkcon remote access over TCP sockets
Expanding method names in tkcon

Development  edit

bugtracker
feature tracker

Obtaining  edit

stevel: tkcon packaged as a starkit can be found at http://www.tcl.tk/starkits/tkcon.kit). 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...
ActiveTcl
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. If TkCon isn't immediately available in the installed ActiveTcl, use teacup to install it.
WinTclTk
TkCon is included.

Unofficial standalone TkCon (with Tclkit) :

A single-file Tkcon executable for 32-bit Microsoft Windows Operating Systems wrapped using TkWrap (part of WinTclTk) can be found at http://wintcltk.sourceforge.net/tkwrap.html

Description  edit

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),

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.5 There is also a megawidget console based on tkcon.

It's often useful to use Img with TkCon:
tkcon eval package require [Img]

Installation  edit

Make a directory (e.g. tkcon-2.5) in your Tcl lib directory, sibling to other packages. Copy the pkgIndex.tcl and tkcon.tcl files there. Load the package and launch the console:
package require tkcon
tkcon show

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) ""

In a comp.lang.tcl posting, Glenn Jackman offers his personal tkcon.cfg source:
# tkcon.cfg

tkcon font "Lucida Console" 10
set ::tkcon::OPT(rows) 40

# source $env(HOME)/tclshrc.tcl
# hmmm, can't load procs in this config file.
# ah:
set ::tkcon::OPT(slaveeval) {source $env(HOME)/tclshrc.tcl}

kpv: Here's my tkcon.cfg file as a more complicated example:
set ::tkcon::OPT(rows) 14
set ::tkcon::OPT(cols) 75
set ::tkcon::OPT(font) "Courier 12"
set ::tkcon::OPT(history) 300
set ::tkcon::OPT(autoload) Tk
set ::tkcon::OPT(calcmode) 1
set ::tkcon::OPT(maxlinelen) 500
   
set ::tkcon::OPT(slaveeval) {
    source c:/home/.wishrc
    package require Img
    wm protocol . WM_DELETE_WINDOW exit
}
set ::tkcon::OPT(maineval) {
    wm geom . +140+825
    bind TkConsole <Control-p> [bind TkConsole <<TkCon_PreviousSearch>>]
    bind TkConsole <Up>        [bind TkConsole <<TkCon_PreviousSearch>>]
    bind TkConsole <Control-n> [bind TkConsole <<TkCon_NextSearch>>]
    bind TkConsole <Down>      [bind TkConsole <<TkCon_NextSearch>>]
}

hv: On my Linux Mint 11, I found the following font and color works best for me:
set ::tkcon::COLOR(bg) ivory
set ::tkcon::OPT(font) "{Liberation Mono} 10"
set ::tkcon::OPT(rows) 40
set ::tkcon::OPT(cols) 80

adrian: tkcon version 2.6 on OSX has a bug that prevents accessing the command history via the arrow up key. Emiliano pointed out that the newer version of tkcon here fixes this issue.

Open Questions  edit

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 [1], observe [2], "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.

NEM: A version of RS's 'see' proc for viewing images inline in the console, adapted for TkCon:
proc see image {
    set im [tkcon eval image create photo -file $image]
    tkcon console image create end -image $im
    tkcon console insert end \n
}

RLH 2011-09-21: Can tkcon take color values in hex besides the actual color name?

AMG: I don't see why not. Just prefix the color value with #, as in #aaa or #ff0000. See the Tk_GetColor() documentation.

milarepa 2013-10-18 03:09:47:

How I can add command line history using the up and down keys? For tclsh there are a few readline libraries that can achieve this, but nothing for Tkcon.

IDG: If you are using tcl 8.6, get a more current version of Tkcon. History recall was always part of tkcon, but was broken by changes to text in 8.6, and subsequently repaired.

kap: A minimalist, easy on the eyes, .tkconrc / tkcon.cfg dark theme.
# Disable menu and statusbar
set ::tkcon::OPT(showmenu)      0
set ::tkcon::OPT(showstatusbar) 0
# Configure font and colors
set ::tkcon::OPT(font)     {Consolas 10}
set ::tkcon::COLOR(bg)     gray10
set ::tkcon::COLOR(cursor) ivory
set ::tkcon::COLOR(stdin)  ivory
set ::tkcon::COLOR(proc)   deepskyblue
set ::tkcon::COLOR(prompt) green3
set ::tkcon::COLOR(stdout) ivory
set ::tkcon::COLOR(var)    darkred
set ::tkcon::COLOR(blink)  goldenrod4
set ::tkcon::COLOR(stderr) red

AMG: Is there any way to embed Tkcon into an existing toplevel window alongside other widgets? I'd like to use it in place of a [text] widget whose purpose is to collect logs ([puts] would work well for that) and provide interactive debugging and development. The [tkcon show -root .whatever] option only succeeds in specifying the name of the toplevel created by Tkcon.

Unofficial Bug Fixes for Tkcon v2.5 on Tcl/Tk 8.6.x  edit

The latest version of Tkcon on SourceForge is v2.5 (CVS v1.102 2008/02/07 21:02:53). Cripes! OK, well "if it ain't broke, don't fix it". Oh wait, it is broke! Let's fix it!

Just downloading and using this version has several bugs (Verified on Windows 10 with Magicsplat 8.6 and Debian 9 Tcl/Tk 8.6):

  • Hot Errors trigger an error "unknown option "-under""
     [DPG]: This is a super-easy fix of just changing all references of -under to -underline

  • Up and Down keys do not trigger <<TkCon_Previous>> and <<TkCon_Next>> when on the prompt line
     [DPG]: the built-in virtual events <<NextLine>> and <<PrevLine>> mess up the bindings on the custom virtual events <<TkCon_Previous>> and <<TkCon_Next>>
     To fix it, I added the following lines right after the virtual event binding sections (file line 5064)
     bind TkConsole <<NextLine>> {}
     bind TkConsole <<PrevLine>> {}

  • Help > Retrieve Latest Version doesn't work anymore (and can destroy your tkcon.tcl file if you click through the warnings!)
     [DPG]: SourceForge has is moving away from CVS (https://sourceforge.net/blog/decommissioning-cvs-for-commits/).
     It appears that HTTP access for CVS is gone, which means that the Tkcon method of updating won't work properly with SourceForge.  Time to update to another VCS or change hosting.

  • The archive has a README.txt and a release.txt which are almost identical.
     [DPG]: I removed release.txt in my personal repo and updated the README.txt to say "Tested through Tcl/Tk 8.6"

I'd be happy to patch the official sources if I had access.

DPG Turns out, there is a repo with the above fixes (except for CVS checkout on SourceForge): https://github.com/wjoye/tkcon