如何在 Tkinter 应用程序中嵌入终端? [英] How to embed a terminal in a Tkinter application?

查看:78
本文介绍了如何在 Tkinter 应用程序中嵌入终端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Tkinter 主窗口中嵌入一个终端.我想要一个子窗口,可以在其中运行终端(基于 Bash 的终端).我还希望能够让我的程序与终端交互,至少我想读取当前工作目录和/或设置它.

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.

不知道是不是真的不可能.过去我可以用 Perl/Tk 做到这一点,所以也许它可以在这里复制.

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 &"); 

其中 $mw 是主 Tk 窗口.

where $mw was the main Tk window.

当然,我完全同意 Bryan:虽然我以前从未用 GUI 库编程,但我的程序(相当大,一种 wiki)运行得非常好,用于 GUI 本身的代码量少得惊人.

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.

我尝试翻译此 Perl 代码,但遇到了 ID 问题.

I tried translating this Perl code, but I'm stumbling on the ID problem.

我在 Effbot 中找到了从 Tkinter 中提取 ID 的方法的唯一参考a>,但是当我使用它时,我得到'AttributeError: Frame instance has no attribute 'window_id',所以一定有什么问题:

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);  

推荐答案

我很高兴地说,事实上它是可以做到的,而且你只需几行代码就可以做到(我不不知道使用其他工具包是否如此简单):

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):

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()

之前的问题是对wid使用了错误的函数.

The problem before was to use the wrong function for wid.

这篇关于如何在 Tkinter 应用程序中嵌入终端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆