问题描述
有一个可以通过 Bash 终端使用的现有环境和框架,我想围绕它制作一个 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:
- 在 Bash 会话中,设置了框架环境.这导致在会话中设置从环境变量到身份验证的所有内容.
- 运行 Python GUI 脚本以环绕现有会话并使其更容易运行后续步骤.
- GUI 出现,一侧显示嵌入式终端中的 Bash 会话,另一侧显示一组按钮,这些按钮对应于可在现有框架环境中运行的各种命令.
- 可以在 GUI 中按下按钮,从而运行某些 Bash 命令.运行结果显示在嵌入式终端中.
创建此类 GUI 的好方法是什么?我意识到与现有环境交互的想法可能很棘手.如果它特别棘手,我愿意在 GUI 会话中重新创建环境.无论如何,GUI如何与嵌入式终端交互.当GUI的按钮被按下时,如何在嵌入式终端中运行和显示命令?
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?
GUI(具有嵌入式终端)的基本开始如下:
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_())
在 GUI 中按下按钮后,我如何在这个嵌入式终端上运行 top
?
How could I run, say, top
on this embedded terminal following the press of a button in the GUI?
推荐答案
如果它必须成为一个真正的终端和一个真正的 shell(而不仅仅是接受一行输入,运行一些命令,然后显示输出)——tmux 怎么样?
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?
您可以使用诸如 tee
之类的东西将输出返回到您的程序中.
You could use something like tee
to get the output back into your program.
请注意,tmux 会话可能会在您的程序运行中持续存在,因此您需要详细了解其工作原理以及如何控制它.
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_())
这篇关于如何使用嵌入在 PyQt GUI 中的终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!