执行包含来自Linux终端的子进程的Pyqt5GUI会导致GUI中出现黑屏并冻结i

2024-05-16 18:30:29 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我想向你们展示目前为止的工作原理。下面是一个简单的GUI,其构建原理与引起问题的GUI相同。它有一个按钮,当你点击它计数器增加。你知道吗

#!/usr/bin/python3.5

import sys
from PyQt5 import QtWidgets

class GUI(QtWidgets.QWidget):

    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.initGUI()
        self.behaviours()

        self.counter = 0

    def initGUI(self):
        self.button = QtWidgets.QPushButton("Button")
        self.label = QtWidgets.QLabel()

        self.box = QtWidgets.QVBoxLayout()
        self.box.addWidget(self.button)
        self.box.addWidget(self.label)

        self.setLayout(self.box)
        self.show()

    def behaviours(self):
        self.button.clicked.connect(self.add)

    def add(self):
        self.counter = self.counter + 1
        self.label.setText(str(self.counter))

app = QtWidgets.QApplication(sys.argv)
ex = GUI()
sys.exit(app.exec_())

我可以使用以下命令从Linux终端执行脚本:

python3 TestGUI.py

GUI按预期打开,我可以与按钮交互。你知道吗

一旦脚本中包含了子进程(如下面的一个),GUI仍然会打开,但它是完全黑色的,没有响应。你知道吗

p1 = subprocess.Popen("onedrive", stdout = subprocess.PIPE, shell = True)
(output, err) = p1.communicate()

我认为当您使用终端执行python脚本时会出现问题,python脚本本身会在终端中执行命令。你知道吗

你知道如何解决这个问题吗?你知道吗

非常感谢您的支持。你知道吗


Tags: importself脚本box终端defsyscounter
1条回答
网友
1楼 · 发布于 2024-05-16 18:30:29

您不应该使用Popen,因为^{}方法是阻塞的,而应该使用^{}

#!/usr/bin/python3.5

from PyQt5 import QtCore, QtWidgets


class GUI(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.initGUI()
        self.behaviours()

    def initGUI(self):
        self.button = QtWidgets.QPushButton("Button")
        self.label = QtWidgets.QLabel()

        box = QtWidgets.QVBoxLayout(self)
        box.addWidget(self.button)
        box.addWidget(self.label)

        self.show()

    def behaviours(self):
        self._onedrive_process = QtCore.QProcess(self)
        self._onedrive_process.setProcessChannelMode(QtCore.QProcess.MergedChannels)
        self._onedrive_process.readyReadStandardOutput.connect(
            self.on_readyReadStandardOutput
        )
        self._onedrive_process.setProgram("onedrive")

        self.button.clicked.connect(self.connect_to_onedrive)

    @QtCore.pyqtSlot()
    def connect_to_onedrive(self):
        self._onedrive_process.start()

    @QtCore.pyqtSlot()
    def on_readyReadStandardOutput(self):
        result = self._onedrive_process.readAllStandardOutput()
        print(result)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    ex = GUI()
    sys.exit(app.exec_())

更新:

如果要将选项传递给命令,则必须使用^{}

from PyQt5 import QtCore, QtGui, QtWidgets


class OneDriveManager(QtCore.QObject):
    logChanged = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super().__init__(parent)
        self._process = QtCore.QProcess(self)
        self._process.readyReadStandardOutput.connect(self.on_readyReadStandardOutput)
        self._process.setProgram("onedrive")

    def launch(self, options=None):
        self._process.setArguments(options)
        if self._process.state() != QtCore.QProcess.NotRunning:
            self._process.kill()
        self._process.start()

    def help(self):
        self.launch([" help"])

    def synchronize(self):
        self.launch([" synchronize"])

    @QtCore.pyqtSlot()
    def on_readyReadStandardOutput(self):
        res = self._process.readAllStandardOutput().data().decode()
        self.logChanged.emit(res)


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self._onedrive_manager = OneDriveManager(self)

        help_button = QtWidgets.QPushButton("Help")
        help_button.clicked.connect(self._onedrive_manager.help)

        synchronize_button = QtWidgets.QPushButton("Synchronize")
        synchronize_button.clicked.connect(self._onedrive_manager.synchronize)

        log_plaintextedit = QtWidgets.QPlainTextEdit()
        self._onedrive_manager.logChanged.connect(log_plaintextedit.setPlainText)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(help_button)
        lay.addWidget(synchronize_button)
        lay.addWidget(log_plaintextedit)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    w = Widget()
    w.show()

    sys.exit(app.exec_())

相关问题 更多 >