如何在Tkinter应用中嵌入终端?
我想在我的主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的看法:虽然我之前从没用过图形界面库编程,但我的程序(其实是一个相当大的维基类项目)运行得很好,写给图形界面的代码量出乎意料地少。
我试着把这段Perl代码翻译过来,但在ID的问题上遇到了困难。
我找到的唯一关于如何从Tkinter中提取ID的参考资料是在Effbot上,但当我使用它时,出现了'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);
2 个回答
0
阿莱桑德罗在五小时前就提到他认为合适的模型了。为了方便以后搜索到这个内容的人,我想记录一些我知道的背景信息:
很幸运布莱恩在这里,指出了window_id()和winfo_id()之间的区别,并纠正了其他人在写各种工具包时犯的错误。
我觉得stackoverflow和一些更专业的渠道相比,挺有意思的。在这个案例中,Tkinter的邮件列表 http://mail.python.org/pipermail/tkinter-discuss/2011-September/002968.html 很快而且准确地回答了这个问题。
Tkinter在某种程度上会比一些复杂的软件更好。
37
我很高兴地告诉大家,这确实是可以做到的,而且只需要几行代码就可以实现(我不知道其他工具包是否也这么简单):
from Tkinter import *
import os
root = Tk()
termf = Frame(root, height=400, width=500)
termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()
os.system('xterm -into %d -geometry 40x20 -sb &' % wid)
root.mainloop()
之前的问题是用错了计算宽度的函数。