問題描述
我最近一直在努力在 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_())
這篇關于如何在沒有 QProcess 的情況下將終端嵌入 PyQt5 應用程序中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!