Insert xterm into QWidget and chat with it

I want to insert xterminto a widget pyqt4and share data with it. Especially I want to be able to print on it and execute commands on it (so that it returns to the usual user prompt after the command is executed just like a regular shell). 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_())
+3
source share
1 answer

To paste xterminto one of your windows, you should use:

-into windowId X ( ), xterm . xterm .

xterm (bash ..). , . filedescriptors xterm -Sccn:

xterm

, , bash, zsh, , . stdout/stderr fd xterm stdin , , xterm, , bash ( xterm).

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

manpage urxvt , urxvt :

- windowid
urxvt , . [...] Gtk2-perl, , ( doc/embed):

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

-pty-fd
urxvt - pty/tty, tty. , urxvt .

perl, , ( p > doc/pty-fd):

IO:: Pty;
Fcntl;

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

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

PTY python, pty : http://docs.python.org/2/library/pty.html

: http://sqizit.bartletts.id.au/2011/02/14/pseudo-terminals-in-python/

+4

Source: https://habr.com/ru/post/1780395/


All Articles