本文介绍了将xterm嵌入QWidget并与其通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将xterm嵌入到pyqt4小部件中并与其通信.特别是我希望能够打印到它并在其上执行命令(以使它在执行命令后像普通的shell一样返回到正常的用户提示符).考虑下面的最小示例.我该如何运作?

I want to embed an xterm into a pyqt4 widget and communicate with it. Especially I want to be able to print to it and execute commands on it (such that it returns to normal user prompt after executing the command just like a normal shell would do). Consider the following minimal example. How can I make it work?

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import  sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class embedxterm(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        self.setMinimumWidth(900)
        self.setMinimumHeight(400)
        self.process = QProcess(self)

        self.terminal = QWidget(self)
        self.terminal.setMinimumHeight(300)

        self.cmd1 = QPushButton('Command1',self)
        self.cmd2 = QPushButton('Command2',self)
        self.hello = QPushButton('Print Hello World',self)

        layout = QVBoxLayout(self)

        layoutH = QHBoxLayout(self)

        layoutH.addWidget(self.cmd1)
        layoutH.addWidget(self.cmd2)
        layoutH.addWidget(self.hello)


        layout.addLayout(layoutH)
        layout.addWidget(self.terminal)


        self.process.start(
            'xterm',['-into', str(self.terminal.winId())])

        self.cmd1.clicked.connect(self.Ccmd1)
        self.cmd2.clicked.connect(self.Ccmd2)
        self.hello.clicked.connect(self.Chello)

    def Ccmd1(self):
        self.process.write('ls -l')
        # Should execute ls -l on this terminal

    def Ccmd2(self):
        self.process.write('ping www.google.com')
        # should execute ping www.google.com on this terminal

    def Chello(self):
        self.process.write('Hello World')
        # should just print "Hello World" on this terminal

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = embedxterm()
    main.show()
    sys.exit(app.exec_())

推荐答案

要将xterm嵌入到您的一个窗口中,您应该使用:

To embed a xterm into one of your windows you should use:

xterm本身与启动的shell(bash等)对话.因此,您必须找到一种与已启动的Shell对话的方法.您可以通过-Sccn标志将打开的文件描述符传递给xterm:

xterm itself talks to the launched shell (bash etc). So, you have to find a way to talk to that launched shell. You can pass open filedescriptors to xterm via the -Sccn flag:

因此,我想您必须创建bash,zsh实例,无论您要将命令发送到哪个实例.然后将该子进程的stdout/stderr fd连接到您的xterm实例,并将stdin连接到您的主程序,然后将来自xterm的输入和您要发送到bash的命令多路复用(这样它们将被执行并被显示在xterm中.)

So, I think you have to create your instance of bash, zsh, whatever you want to send the commands to. Then connect the stdout/stderr fd of that subprocess to your instance of xterm and connect the stdin to your main program which then multiplexes the input coming from the xterm and the commands you want to send to the bash (so they will get executed and be shown in xterm).

bash ----------------------> xterm
    \--< your.py <----------/

urxvt的联机帮助页显示,urxvt具有一些类似的开关:

The manpage of urxvt reveils that urxvt has some similar switches:

我的$ rxvt =新的Gtk2 :: Socket;
$ rxvt-> signal_connect_after(实现=>子{ 我的$ xid = $ _ [0]->窗口-> get_xid;
系统"urxvt -embedd $ xid&";
});

my $rxvt = new Gtk2::Socket;
$rxvt->signal_connect_after (realize => sub { my $xid = $_[0]->window->get_xid;
system "urxvt -embed $xid &";
});

以下是perl中的一个示例,说明了如何使用此选项(更长的示例>在doc/pty-fd中):

Here is a example in perl that illustrates how this option can be used (a longer example > is in doc/pty-fd):

使用IO :: Pty;
使用Fcntl;

use IO::Pty;
use Fcntl;

我的$ pty =新的IO :: Pty;
fcntl $ pty,F_SETFD,0; #清除close-on-exec
系统"urxvt -pty-fd". (fileno $ pty). &";
关闭$ pty;

my $pty = new IO::Pty;
fcntl $pty, F_SETFD, 0; # clear close-on-exec
system "urxvt -pty-fd " . (fileno $pty) . "&";
close $pty;

#现在与rxvt通信
我的$ slave = $ pty-> slave;
while(){print $ slave"got \ n"}

# now communicate with rxvt
my $slave = $pty->slave;
while () { print $slave "got \n" }

要从python中打开PTY,pty模块看起来很有希望: http://docs.python.org/2/library/pty.html

To open a PTY from within python the pty module looks promising: http://docs.python.org/2/library/pty.html

有趣的内容: http://sqizit. bartletts.id.au/2011/02/14/pseudo-terminals-in-python/

这篇关于将xterm嵌入QWidget并与其通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 09:25