Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use a terminal embedded in a PyQt GUI

There is an existing environment and framework usable via Bash terminal around which I want to make a GUI. What I have in mind is the following flow:

  • In a Bash session, the framework environment is set up. This results in everything from environment variables to authentications being set up in the session.
  • A Python GUI script is run in order to wrap around the existing session and make it easier to run subsequent steps.
  • The GUI appears, displaying on one side the Bash session in an embedded terminal and on the other side a set of buttons corresponding to various commands that can be run in the existing framework environment.
  • Buttons can be pressed in the GUI resulting in certain Bash commands being run. The results of the runs are displayed in the embedded terminal.

What is a good way to approach the creation of such a GUI? I realise that the idea of interacting with the existing environment could be tricky. If it is particularly tricky, I am open to recreating the environment in a session of the GUI. In any case, how can the GUI interact with the embedded terminal. How can commands be run and displayed in the embedded terminal when buttons of the GUI are pressed?

A basic start of the GUI (featuring an embedded terminal) is as follows:

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

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

class embeddedTerminal(QWidget):

    def __init__(self):

        QWidget.__init__(self)
        self.resize(800, 600)
        self.process  = QProcess(self)
        self.terminal = QWidget(self)
        layout = QVBoxLayout(self)
        layout.addWidget(self.terminal)
        self.process.start(
            'xterm',
            ['-into', str(self.terminal.winId())]
        )

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

How could I run, say, top on this embedded terminal following the press of a button in the GUI?

like image 622
d3pd Avatar asked Mar 18 '15 00:03

d3pd


People also ask

Is PyQt good for GUI?

PyQt5 is a very well-known GUI framework used by both Python coders and UI designers. One of its components, the PyQt package, is built around the Qt framework, which is a leading cross-platform GUI design tool for just about any kind of application.

Is PyQt an IDE?

Wing Pro is a Python IDE that can be used to develop, test, and debug Python code written for the PyQt cross-platform GUI development toolkit.

Does PyQt require Qt?

You can purchase the commercial version of PyQt here. More information about licensing can be found in the License FAQ. PyQt does not include a copy of Qt. You must obtain a correctly licensed copy of Qt yourself.


1 Answers

If it has to be a real terminal and a real shell (and not just accepting a line of input, running some command, then displaying output) -- how about tmux?

You could use something like tee to get the output back into your program.

Note that tmux sessions may persist across your program runs, so you'd need to read up on how that works and how to control it.

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

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

class embeddedTerminal(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        self._processes = []
        self.resize(800, 600)
        self.terminal = QWidget(self)
        layout = QVBoxLayout(self)
        layout.addWidget(self.terminal)
        self._start_process(
            'xterm',
            ['-into', str(self.terminal.winId()),
             '-e', 'tmux', 'new', '-s', 'my_session']
        )
        button = QPushButton('List files')
        layout.addWidget(button)
        button.clicked.connect(self._list_files)

    def _start_process(self, prog, args):
        child = QProcess()
        self._processes.append(child)
        child.start(prog, args)

    def _list_files(self):
        self._start_process(
            'tmux', ['send-keys', '-t', 'my_session:0', 'ls', 'Enter'])

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

A bit more here: https://superuser.com/questions/492266/run-or-send-a-command-to-a-tmux-pane-in-a-running-tmux-session

like image 199
Croad Langshan Avatar answered Nov 07 '22 10:11

Croad Langshan