简体   繁体   English

“单击按钮后GUI变得无响应”

[英]“GUI becomes unresponsive after clicking the button”

I've designed a GUI for my application and after a button click, I've connected a custom function of mine wherein I call a bash command to run 2 Python scripts simultaneously. 我为我的应用程序设计了一个GUI,单击按钮后,我连接了我的自定义函数,在其中调用bash命令以同时运行2个Python脚本。 Both the scripts are Qt applications. 这两个脚本都是Qt应用程序。 I have a button named 'Start' to run the bash command. 我有一个名为“开始”的按钮来运行bash命令。 But as soon as I have clicked the button, both the applications start but my GUI freezes. 但是,一旦单击按钮,两个应用程序都启动,但我的GUI冻结。 Also, I have a 'Stop' button to stop these applications, but the GUI freezes and I'm unable to do anything with the GUI. 另外,我有一个“停止”按钮来停止这些应用程序,但是GUI冻结,并且我无法使用GUI进行任何操作。 What am I doing wrong? 我究竟做错了什么?

I've tried various options for running 2 scripts simultaneously, but the command python3 script1.py & python3 script2.py & worked fine for me. 我尝试了多种选项来同时运行2个脚本,但是命令python3 script1.py & python3 script2.py &对我来说很好用。 Are there any other ways around? 还有其他方法吗? The os.system() or subprocess.call() didn't work out for me. os.system()subprocess.call()并没有为我工作了。 Are there any efficient ways to do so? 有什么有效的方法吗?

This is my main code for my UI. 这是我的UI的主要代码。

class MainApp(QtWidgets.QMainWindow, mainui.Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainApp, self).__init__(parent)
        self.setupUi(self)
        self.exitapp()
        self.startApp()


    def multipleopen(self):
        self.output = subprocess.check_output(['bash', '-c', 
                      "python3 guiGO.py & python3 liveSpectogram.py &"])
    def startApp(self):
        self.start.clicked.connect(self.multipleopen)

    def exitapp(self):
      self.exit.clicked.connect(QtCore.QCoreApplication.instance().quit)

I'm expecting to start both applications on clicking the 'Start' button and stop it by clicking the 'Stop' button. 我希望在单击“开始”按钮时启动两个应用程序,并通过单击“停止”按钮将其停止。 But after clicking the 'Start' button the GUI freezes and I'm unable to do anything but close the running applications and close the GUI window 但是,单击“开始”按钮后,GUI冻结,除了关闭正在运行的应用程序并关闭GUI窗口外,我无法执行任何操作

GUI becomes unresponsive GUI变得无响应

I've to force quit the GUI 我必须强制退出GUI

I have to stop the script manually using the stop button 我必须使用“停止”按钮手动停止脚本

The function subprocess.check_output() is blocking, so it will prevent the event-loop of the GUI from doing its job by freezing your application, instead you should use QProcess : subprocess.check_output()函数正在阻塞,因此它将通过冻结应用程序来防止GUI的事件循环执行其工作,而应使用QProcess

class MainApp(QtWidgets.QMainWindow, mainui.Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainApp, self).__init__(parent)
        self.setupUi(self)

        self._process = QtCore.QProcess(self)
        self._process.readyReadStandardOutput.connect(self.on_readyReadStandardOutput)
        self._process.setProcessChannelMode(QtCore.QProcess.ForwardedChannels)
        self._process.setProgram('bash')
        self._process.setArguments(['-c', 'python3 guiGO.py & python3 liveSpectogram.py &'])

        self.exitapp()
        self.startApp()

    # read prints
    def on_readyReadStandardOutput(self):
        self.output = self._process.readAllStandardOutput()
        print(self.output)

    def startApp(self):
        self.start.clicked.connect(self._process.start)

    def exitapp(self):
        self.exit.clicked.connect(self.close)

Update: 更新:

If you want to kill the python scripts you must do it through bash, with the pkill command, since this was the one that launched them. 如果您想杀死python脚本,则必须使用pkill命令通过bash进行,因为这是启动它们的脚本。 And in what part of the code should you call pkill ?, a possible part is the closeEvent method. 您应该在代码的哪一部分叫pkill?,可能的一部分是closeEvent方法。

from PyQt5 import QtCore, QtWidgets

class MainApp(QtWidgets.QMainWindow): #, mainui.Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainApp, self).__init__(parent)
        self.setupUi(self)

        self._scripts = ("guiGO.py", "liveSpectogram.py")
        self._processes = []
        for script in self._scripts:
            process = QtCore.QProcess(self)
            process.readyReadStandardOutput.connect(self.on_readyReadStandardOutput)
            process.setProcessChannelMode(QtCore.QProcess.ForwardedChannels)
            process.setProgram('bash')
            process.setArguments(['-c', 'python3 {}'.format(script)])
            self._processes.append(process)

        self.exitapp()
        self.startApp()

    # read prints
    def on_readyReadStandardOutput(self):
        self.output = self.sender().readAllStandardOutput()
        print(self.output)

    def startApp(self):
        for process in self._processes:
            self.start.clicked.connect(process.start)

    def exitapp(self):
        self.exit.clicked.connect(self.close)

    def closeEvent(self, event):
        for script in self._scripts:
            QtCore.QProcess.startDetached("bash", ["-c", "pkill -f {}".format(script)])
        super(MainApp, self).closeEvent(event)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainApp()
    w.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮查看pdf后,Tkinter GUI变得无响应 - Tkinter GUI become unresponsive after clicking button to view pdf 单击 Tkinter 中的按钮时程序无响应? - Program unresponsive on clicking a button in Tkinter? Flask:服务器在一段时间后变得没有响应 - Flask: Server becomes unresponsive after some time Pygame 运行几秒后无响应 - Pygame Becomes Unresponsive after running for a few seconds 单击显示屏外时,pygame 显示屏变得无响应 - pygame display becomes unresponsive when clicking outside of the display PyQt5:如何使单击后的按钮关闭GUI - PyQt5 : How to make a button close the gui after clicking 单击后更新按钮的位置? (Tkinter Python GUI) - Updating the position of a button after clicking? (Tkinter Python GUI) Jupyter Notebook在闲置一段时间后变得没有响应,不会登录到终端 - Jupyter Notebook becomes unresponsive after inactivity for a while, does not log to terminal RabbitMQ 交换在一段时间后变得无响应 - RabbitMQ exchange becomes unresponsive after some amount of time 脚本在工作期间变得无响应,但之后继续工作并正确结束 - Script becomes unresponsive during work, but continues to work after that and ends correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM