如何在 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 的观点:虽然我以前从未使用 GUI 库进行编程,但我的程序(相当大,有点像 wiki)运行得非常好,专门用于 GUI 本身的代码量少得惊人。
我尝试翻译此 Perl 代码,但我遇到了 ID 问题。
我唯一找到从 Tkinter 提取 ID 的方法的参考是 Effbot,但是当我使用它时,我得到'AttributeError:框架实例没有属性'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);
I want to embed a terminal in my main Tkinter window. I would like to have a sub window where a terminal (Bash based terminal) would run. I would like also to be able to let my program interact with the terminal, at least I would like to read the current working directory and/or set it.
I don't know if it is really impossible. I was able to do it in the past with Perl/Tk, so maybe it can be replicated here.
The code I used then was something like:
$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 &");
where $mw
was the main Tk window.
Of course, I completely agree with Bryan: though I never programmed with a GUI library before, my program (rather large, a kind of wiki) is running very well, with a surprisingly low amount of code devoted to the GUI itself.
I tried translating this Perl code, but I'm stumbling on the ID problem.
The only place where I found some reference to a way to extract the ID from Tkinter is in Effbot, but when I use it, I get 'AttributeError: Frame instance has no attribute 'window_id'
, so there must be something wrong:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很高兴地说,实际上是可以做到的,而且只需几行代码就可以做到(我不知道使用其他工具包是否这么容易):
之前的问题是对wid使用了错误的函数。
I am happy to say that it is in fact possible to do it, and you can do it with just a few lines of code (I don't know if it is so easy with other toolkits):
The problem before was to use the wrong function for wid.
亚历山德罗(Alessandro)已经在他认为合适的模型之前五个小时报告了。对于那些在以后的搜索中遇到这个项目的人,我将记录更多我所知道的背景事实:
幸运的是 Bryan 在这里提请注意 window_id() 和 winfo_id() 之间的差异,并纠正错误其他人则撰写了有关各种工具包的文章。
我对 stackoverflow 与更专业的频道进行比较很感兴趣。在本例中,Tkinter 邮件列表 http://mail.python .org/pipermail/tkinter-discuss/2011-September/002968.html迅速准确地回答了问题。
Tkinter 至少是对某些登月火箭软件的改进。
Alessandro already reported five hours before what he regards as an adequate model. For those who come across this item during future searches, I'll record a few more background facts I know:
It was fortunate that Bryan was here to draw attention to the differences between window_id() and winfo_id(), and to counter the errors others made in writing about various toolkits.
It's interesting to me how stackoverflow compares to more specialized channels. In this case, the Tkinter mailing list http://mail.python.org/pipermail/tkinter-discuss/2011-September/002968.html swiftly and accurately responded to the question.
Tkinter would be an improvement on at least some of the moon-rocket software.