如何在没有QProcess的情况下将终端嵌入PyQt5应用程序? [英] How to embed terminal inside PyQt5 application without QProcess?

查看:821
本文介绍了如何在没有QProcess的情况下将终端嵌入PyQt5应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在努力将终端嵌入PyQt GUI应用程序中.几乎尝试了Internet上的所有搜索,但似乎没有任何帮助.

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.

是否完全不可能这样做?

Is it not at all possible to do so ?

是否没有QTabWidget.Tab2.show(terminal-app)之类的东西,默认终端显示在tab2中,并且lsifconfigcd之类的每个功能都工作正常?

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,但这不能满足我的需求)

(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应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆