2
votes

I want to make a Tcl/Tk application that is--mostly--a conventional menus-and-buttons direct manipulation tool, where most of the interaction is through a graphical interface implemented in Tcl/Tk.

However, for certain advanced uses (and debugging), I'd like to have a widget (subwindow) within the main window that contains a Tk console where I can type commands, see output, and otherwise control the application.

It seems easy enough to start TkCon (or wish) and get one top-level window, then create my application interface in a separate top-level window. The application will work fine that way, but I'd like the two windowso be part of the same layout, to move together, to support resizing, etc.

Is there an easy way to do this with TkCon?

I'd also like the TkCon window to be able to display messages that bubble up from within my application (e.g., debug output). Some messages would be generated by Tcl code; others by C code that makes up part of my application. I don't need to capture stdout as such--I'm willing to call a special-purpose function to deliver the messages--but it's not clear what's the most effective way to to get them to display like that.

3

3 Answers

1
votes

For tkcon specifically see Donal's answer. I will add however that you can embed the Tk built-in console that is used on Windows. This script is available on non-Windows and can be made to embed into a tabbed notebook page for example. See tkchat_console.tcl for an example of this - the file loads the Tk console.tcl file and the ::tkchat::EmbeddedConsoleDemo function at the bottom shows how you might use this.

1
votes

The following code works for me:

set f [labelframe $p.console -text "Interactive:"]
frame $f.test -container 1
namespace eval ::tkcon {
    set OPT(exec) {}
    set PRIV(root) .tkcon
    set embed_args {}
}
option add *tkcon.Use [winfo id $f.test] interactive
package require tkcon
tkcon::Init

This code addse -use option to tkcon toplevel via "X11 options". ::tkcon::embed_args is also vital.

0
votes

Reading the documentation I only see ways to make it work officially as its own toplevel window. (In particular, tkcon new doesn't take any arguments…) So we're talking a hack to get what you want.

If you've got Tk 8.6 and aren't on OSX (or are using an X11-based build on that platform), you can morph the toplevel into a frame with wm forget and embed that way, but I don't know if the lack of control over the widget name in that case will hurt.

Otherwise, if you've got BLT available I believe that has the ability to reparent widgets. I've never tried doing that so this is hearsay, but it might be able to put a toplevel inside another widget.

Getting more hacky, you could edit the tkcon sources so that you can specify the -use option to the toplevel it creates. That would let you place it in another widget (a frame with the -container option turned on; you'd have to piece things together with winfo id too) but again, it's a bit complex and I don't know what the consequences of doing this are on your platform. This should work on older versions of Tk (it was the foundation of how the Tcl/Tk browser plugin functioned).