如何在没有 QProcess 的情况下将终端嵌入 PyQt5 应用程序中?
问题描述
我最近一直在努力在 PyQt GUI 应用程序中嵌入终端.几乎尝试了互联网上的所有搜索,但没有任何帮助.
I have been struggling lately with embedding a terminal inside PyQt GUI app. Tried almost every search on Internet but nothing looks like of any help.
我有一个 QTabWidget,我只需要一个标签就有一个终端.
I have a QTabWidget and I simply need one tab to have a terminal.
根本不可能这样做吗?
难道没有像 QTabWidget.Tab2.show(terminal-app)
这样的东西吗?默认终端会显示在 tab2 中,每个函数都像 ls
、ifconfig
、cd
等工作正常吗?
Isn't there something like QTabWidget.Tab2.show(terminal-app)
and default terminal gets displayed in tab2 and every function like ls
, ifconfig
, cd
etc works fine ?
P.S - 我已经尝试过这些但没有成功.在 PyQt5 中嵌入终端
P.S - I have already tried these but no success. Embedding a terminal in PyQt5
(此处将代码从 PyQt4 转换为 PyQt5,但这不能满足我的需求)如何使用嵌入在 PyQt GUI 中的终端
(converted code here from PyQt4 to PyQt5 but this does not fulfill my needs) how to use a terminal embedded in a PyQt GUI
T.I.A
解决方案
简答: Qt5不提供终端的使用,所以你必须使用QProcess.
short answer: Qt5 does not provide the use of the terminal, so you will have to use QProcess.
TL;DR
作为解决方案提出的 EmbTerminal 类是一个小部件,因此您必须使用 addTab()
添加它,请记住您必须已安装 urxvt
终端(如果你想检查你的安装在终端运行 urxvt
)
The EmbTerminal class that is proposed as a solution is a widget so you must add it with addTab()
, keep in mind that you must have installed the urxvt
terminal (if you want to check your installation run urxvt
in the terminal)
import sys
from PyQt5 import QtCore, QtWidgets
class EmbTerminal(QtWidgets.QWidget):
def __init__(self, parent=None):
super(EmbTerminal, self).__init__(parent)
self.process = QtCore.QProcess(self)
self.terminal = QtWidgets.QWidget(self)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.terminal)
# Works also with urxvt:
self.process.start('urxvt',['-embed', str(int(self.winId()))])
self.setFixedSize(640, 480)
class mainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(mainWindow, self).__init__(parent)
central_widget = QtWidgets.QWidget()
lay = QtWidgets.QVBoxLayout(central_widget)
self.setCentralWidget(central_widget)
tab_widget = QtWidgets.QTabWidget()
lay.addWidget(tab_widget)
tab_widget.addTab(EmbTerminal(), "EmbTerminal")
tab_widget.addTab(QtWidgets.QTextEdit(), "QTextEdit")
tab_widget.addTab(QtWidgets.QMdiArea(), "QMdiArea")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main = mainWindow()
main.show()
sys.exit(app.exec_())
相关文章