“GUI becomes unresponsive after clicking the button”

后端 未结 1 428
天命终不由人
天命终不由人 2021-01-23 17:23

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.

1条回答
  •  走了就别回头了
    2021-01-23 17:36

    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:

    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. And in what part of the code should you call pkill ?, a possible part is the closeEvent method.

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

    0 讨论(0)
提交回复
热议问题