如何将 TkCon(或其他 Tk 控制台)嵌入为小部件?
我想制作一个 Tcl/Tk 应用程序,它主要是一个传统的菜单和按钮直接操作工具,其中大部分交互是通过在 Tcl/Tk 中实现的图形界面进行的。
但是,对于某些高级用途(和调试),我希望在主窗口中有一个小部件(子窗口),其中包含 Tk 控制台,我可以在其中键入命令、查看输出以及以其他方式控制应用程序。
启动 TkCon(或希望)并获得一个顶级窗口,然后在单独的顶级窗口中创建我的应用程序界面似乎很容易。这样应用程序就可以正常工作,但我希望两个窗口成为同一布局的一部分,一起移动,支持调整大小等。
有没有一种简单的方法可以使用 TkCon 来做到这一点?
我还希望 TkCon 窗口能够显示从我的应用程序中冒出的消息(例如,调试输出)。一些消息将由 Tcl 代码生成;其他人则通过构成我的应用程序一部分的 C 代码来实现。我不需要捕获这样的标准输出——我愿意调用一个特殊用途的函数来传递消息——但不清楚让它们像这样显示的最有效方法是什么。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 tkcon 具体请参见 Donal 的回答。不过,我要补充的是,您可以嵌入 Windows 上使用的 Tk 内置控制台。该脚本可在非 Windows 上使用,并且可以嵌入到例如选项卡式笔记本页面中。
请参阅 tkchat_console.tcl 是一个示例 - 该文件加载 Tk console.tcl 文件,底部的 ::tkchat::EmbeddedConsoleDemo 函数显示了如何使用它。
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.
以下代码对我有用:
此代码通过“X11选项”将se -use选项添加到tkcon顶层。 ::tkcon::embed_args 也至关重要。
The following code works for me:
This code addse -use option to tkcon toplevel via "X11 options". ::tkcon::embed_args is also vital.
阅读文档我只看到让它正常工作的方法官方作为它自己的顶层窗口。 (特别是,
tkcon new
不接受任何参数...)所以我们正在谈论一种黑客来获得你想要的东西。如果您拥有 Tk 8.6 并且不在 OSX 上(或者在该平台上使用基于 X11 的构建),您可以使用
wm忘记
并以这种方式嵌入,但我不知道是否缺乏控制在这种情况下,超过小部件名称会造成伤害。否则,如果您有 BLT 可用,我相信它能够重新设置父级小部件。我从未尝试过这样做,所以这是传闻,但它可能能够将顶层放在另一个小部件中。
变得更黑客,您可以编辑 tkcon 源,以便可以指定
-use
选项到它创建的顶层。这将允许您将其放置在另一个小部件中(带有 < code>-container 选项已打开;您必须使用winfo id
也),但同样,它有点复杂,我不知道这样做会对您的平台产生什么后果。这应该适用于旧版本的 Tk(它是 Tcl/Tk 浏览器插件功能的基础)。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 withwinfo 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).