我想在我的主要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 &");
Run Code Online (Sandbox Code Playgroud)
$mw主要的Tk窗口在哪里.
当然,我完全赞同Bryan:虽然之前我从未使用过GUI库编程,但我的程序(相当大,一种wiki)运行得非常好,并且用于GUI本身的代码量非常少.
我尝试翻译这个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);
Run Code Online (Sandbox Code Playgroud)
ale*_*dro 24
我很高兴地说它实际上可以做到这一点,你只需几行代码即可完成(我不知道其他工具包是否如此简单):
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()
Run Code Online (Sandbox Code Playgroud)
之前的问题是对wid使用了错误的函数.