【发布时间】:2011-11-07 09:09:53
【问题描述】:
我想在我的 Tkinter 主窗口中嵌入一个终端。我想要一个运行终端(基于 Bash 的终端)的子窗口。我还希望能够让我的程序与终端交互,至少我想读取当前工作目录和/或设置它。
我不知道这是否真的不可能。过去我可以用 Perl/Tk 做到这一点,所以也许可以在这里复制。
我当时使用的代码是这样的:
$frame3=$mw->Frame(-borderwidth=>2, -relief=>'groove', # -label=>'stuff for thought',
-labelBackground=>CADRAWWINCOLOR,-background=>CADRAWWINCOLOR);
$cv=$frame3->Canvas(-height=>$cvheight,-width=>$cvwidth,-background=>CADRAWWINCOLOR,
-bg => CADRAWWINCOLOR,
-relief => 'sunken')->pack(-expand => 1, -fill => 'both');
# this Frame is needed for including the xterm in Tk::Canvas
my $xtermContainer = $cv->Frame(-container => 1);
my $xtid = $xtermContainer->id();
# converting the id from HEX to decimal as xterm requires a decimal Id
my ($xtId) = sprintf hex $xtid;
my $dcontitem = $cv->createWindow($xtermWidth/2,$xtermHeight/2,
-window => $xtermContainer,
-width => $xtermWidth,
-height => $xtermHeight,
-state => 'normal');
system("xterm -into $xtId -fn $fontname -geometry $geometry +sb -bg black -fg white -e ./xtermjob.pl $AAfname 5 &");
$mw 是主要的 Tk 窗口。
当然,我完全同意 Bryan 的观点:虽然我以前从未使用 GUI 库进行编程,但我的程序(相当大,一种 wiki)运行得非常好,专门用于 GUI 本身的代码量非常少.
我尝试翻译此 Perl 代码,但遇到了 ID 问题。
我在Effbot 中找到了从 Tkinter 中提取 ID 的唯一方法的参考,但是当我使用它时,我得到了 'AttributeError: Frame instance has no attribute 'window_id',所以一定有什么问题:
termf = Frame(root)
termf.pack(side=BOTTOM, fill=X)
id=termf.window_id()
os.system("xterm -into %d -fn -misc-fixed-medium-r-normal--8-80-75-75-c-50-iso10646-1 -geometry 150x150+0+0 +sb -bg black -fg white -e /root/.bashrc &" % id);
【问题讨论】:
-
您可以将 vte 嵌入到 pygtk 应用程序中,但我不知道 tkinter 中有类似的东西。您使用 tkinter 有什么特别的原因吗?
-
只是我发现它比其他小部件更容易学习!
-
@Keith:你为什么说 pygtk 有更多的未来? Tkinter 已经存在了 long 时间,并且是 python 核心的一部分。我已经很久没有看到它从 python 中取出了。
-
@Keith:我认为您对“仅此而已”的评论是错误的。我创建了一些基于 Tk 的非常复杂、成功的商业应用程序(诚然,使用 tcl/tk 而不是 python/tkinter)。事实上,许多应用程序不需要 Tkinter 不提供的任何东西,尤其是当您考虑在最新版本的 Tkinter 中添加 ttk 小部件时。
-
@Keith:您似乎暗示 pygtk 可以做到这一点。这个问题 (stackoverflow.com/questions/5993390/…) 询问如何在 pygtk 中做到这一点 - 你能回答它,以便我们可以看到如何在你最喜欢的工具包中做到这一点吗?这是一个很难解决的问题,我想看看你在那个工具包中是如何做到的。