从Linux终端执行包含子进程的PyQt5 GUI会在GUI中产生黑屏并将其冻结 [英] Executing a PyQt5 GUI which includes subprocesses from the Linux terminal causes a black screen in the GUI and freezes it

查看:54
本文介绍了从Linux终端执行包含子进程的PyQt5 GUI会在GUI中产生黑屏并将其冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想向您展示到目前为止有效的方法.下面是一个简单的GUI,其构造原理与引起问题的原理相同.它有一个按钮,当您单击它时,计数器增加.

First, I would like to show you what works so far. Below is a simple GUI built with the same principles as the one that causes problems. It has a button and when you click it a counter increases.

#!/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终端执行脚本:

I am able to execute the script from the Linux terminal with the following command:

python3 TestGUI.py

GUI将按预期方式打开,我可以与按钮进行交互.

The GUI opens as intended and I can interact with the button.

一旦脚本中包含一个子进程(如下面的子进程),GUI仍会打开,但是它是完全黑色的,并且没有响应.

As soon as a subprocess gets included in the script, like the one below, the GUI does still open but it is completely black and non-responsive.

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

我认为当您使用终端执行python脚本时会出现问题,该脚本本身会在终端中执行命令.

I think that there is a problem when you use the terminal to execute a python script which itself executes commands in the terminal.

您对如何解决此问题有任何想法吗?

Do you have any idea on how this problem can be solved?

非常感谢您的支持.

推荐答案

由于

更新:

如果要将选项传递给命令,则必须使用 setArguments() :

If you want to pass options to the command you must use setArguments():

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_())

这篇关于从Linux终端执行包含子进程的PyQt5 GUI会在GUI中产生黑屏并将其冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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