Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将xterm嵌入QWidget并与之通信_Python_Qt4_Pyqt4_Xterm_Terminal Emulator - Fatal编程技术网

Python 将xterm嵌入QWidget并与之通信

Python 将xterm嵌入QWidget并与之通信,python,qt4,pyqt4,xterm,terminal-emulator,Python,Qt4,Pyqt4,Xterm,Terminal Emulator,我想将xterm嵌入pyqt4小部件并与之通信。特别是我希望能够打印到它并在上面执行命令(这样它在执行命令后会像普通shell一样返回到正常的用户提示)。考虑下面的最小例子。我怎样才能让它工作 #!/usr/bin/env python #-*- coding:utf-8 -*- import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class embedxterm(QWidget): def __ini

我想将
xterm
嵌入
pyqt4
小部件并与之通信。特别是我希望能够打印到它并在上面执行命令(这样它在执行命令后会像普通shell一样返回到正常的用户提示)。考虑下面的最小例子。我怎样才能让它工作

#!/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
嵌入其中一个窗口,您应该使用:

-在给定X窗口标识符(十进制整数)的windowId中,xterm将其顶级shell小部件重新分配到该窗口。这是用来 在其他应用程序中嵌入xterm

xterm
本身与启动的shell(bash等)对话。所以,你必须找到一种方法与发射的外壳对话。您可以通过
-Sccn
标志将打开的文件描述符传递给
xterm

此选项允许xterm用作现有程序的输入和输出通道,有时在专用应用程序中使用

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

bash------------------->xterm
\--sub{
my$xid=$\u0]->window->get\uxid;
系统“urxvt-嵌入$xid&”;
});

-pty fd文件描述符
告诉urxvt不要执行任何命令或创建新的pty/tty对,而是使用给定的文件描述符作为tty主控。如果您希望将urxvt作为通用终端仿真器驱动,而不必在其中运行程序,那么这非常有用

下面是一个perl示例,演示了如何使用此选项(doc/pty fd中有一个较长的示例>:

使用IO::Pty
使用Fcntl

my$pty=新IO::pty
fcntl$pty,F_设置FD,0;#执行时清除关闭
系统“urxvt-pty fd”。(文件号$pty)。"&";
关闭$pty

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

要从python中打开PTY,
PTY
模块看起来很有希望:


有趣的阅读:

你能在我的文章的最小示例中实现这一点吗?谢谢,但我没有把所有的部分整合起来。也许有人可以针对我的最小示例或至少pyqt4提供更多详细信息。我的答案中哪一部分你不明白?老实说,在问我的问题之前,我在谷歌上找到了你答案中的大部分信息。问题是,我不知道如何把它组合在一起,从哪里开始。所以我把一个简单的例子放在一起,只是问如何让它工作。如果我坚持使用xterm,我认为您的答案中最相关的部分是首先将我的python应用程序连接到bash等。但是,我不知道如何具体实现这一点(请参阅我的示例)。我还应该补充一点,我是一个绝对的python和pyqt4初学者。不,因为我没有时间为你做这些工作。我仍然觉得你不理解我列出的概念。
bash ----------------------> xterm
    \--< your.py <----------/