Python 单击按钮时 PyQt5 终端命令执行问题

Python PyQt5 Terminal Command execution problem on a button click

我正在尝试创建一个应用程序,它可以在单击按钮时执行嵌入式终端命令。实际问题发生在我单击按钮但没有任何反应时。 我有两个脚本,一个有终端小部件,另一个有主 GUI。任何帮助,将不胜感激。

这是第一个脚本

import sys
from PyQt5 import QtCore, QtWidgets


class EmbTerminal(QtWidgets.QWidget):

    def __init__(self, parent=None):
        super(EmbTerminal, self).__init__(parent)
        self._process = []
        self.start_process('urxvt',['-embed', str(int(self.winId())),"-e","tmux"])

    def start_process(self,prog,options):
        child = QtCore.QProcess(self)
        self._process.append(child)
        child.start(prog,options)

    def run_command(self, command = "ls" ):
        program = "tmux"
        options = []
        options.extend(["send-keys"])
        options.extend([command])
        options.extend(["Enter"])
        self.start_process(program, options)

这是第二个剧本

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(745, 496)
        self.tabWidget = QtWidgets.QTabWidget(Dialog)
        self.tabWidget.setGeometry(QtCore.QRect(100, 190, 561, 261))
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.tabWidget.addTab(EmbTerminal(), "Terminal")
        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.tabWidget.addTab(self.tab_2, "")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(280, 70, 211, 71))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

        self.pushButton.clicked.connect(lambda: EmbTerminal.run_command(EmbTerminal(), "ls"))

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Dialog", "Tab 1"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Dialog", "Tab 2"))
        self.pushButton.setText(_translate("Dialog", "ls"))

from terminal5 import EmbTerminal

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

您应该考虑以下几点:

  • 您不应修改 Qt Designer 生成的代码(除非您了解其逻辑)
  • 不要实现Qt Designer 生成的class 中的逻辑,建议创建一个新的class 继承自适当的widget 并使用另一个class 来填写它。

在你的情况下,问题是 lambda: EmbTerminal.run_command(EmbTerminal(), "ls") 中的 EmbTerminal() 对象仅在 lambda 为 运行 时存在,但 lambda 运行时间很短导致命令不发送导致错误。

综合以上,解决方案是:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(745, 496)
        self.tabWidget = QtWidgets.QTabWidget(Dialog)
        self.tabWidget.setGeometry(QtCore.QRect(100, 190, 561, 261))
        self.tabWidget.setObjectName("tabWidget")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(280, 70, 211, 71))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.pushButton.setText(_translate("Dialog", "ls"))


from terminal5 import EmbTerminal


class Dialog(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)

        self.terminal = EmbTerminal()
        self.tabWidget.addTab(self.terminal, "Terminal")
        self.pushButton.clicked.connect(self.on_clicked)

    @QtCore.pyqtSlot()
    def on_clicked(self):
        self.terminal.run_command("ls")


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Dialog()
    w.show()
    sys.exit(app.exec_())

另一方面,如果您只打算发送命令,则没有必要存储 QProcess,而是使用 QProcess: startDetached 并制作 run_command 一个 class 方法:

class EmbTerminal(QtWidgets.QWidget):
    # ...

    @staticmethod
    def run_command(command = "ls" ):
        program = "tmux"
        options = []
        options.extend(["send-keys"])
        options.extend([command])
        options.extend(["Enter"])
        QtCore.QProcess.startDetached(program, options)