How to stop running PyQt5 program without closing the GUI window?

*爱你&永不变心* 提交于 2020-06-27 16:23:29

问题


The following code pings a website and prints the result in QTextEdit. One button "Run" is used to start the ping. I want to have another button "End", which could stop the ping process while it is running without closing the GUI. But currently, the "End" button closes the whole GUI window. Do you have any thoughts on how to stop the ping but keep the GUI, so I can start the ping again by pressing the "Run" button.

import sys
from PyQt5 import QtCore,QtWidgets

class gui(QtWidgets.QMainWindow):
    def __init__(self):
        super(gui, self).__init__()
        self.initUI()

    def dataReady(self):
        cursor = self.output.textCursor()
        cursor.movePosition(cursor.End)
        cursor.insertText(str(self.process.readAll()))
        self.output.ensureCursorVisible()

    def callProgram(self):
        # run the process
        # `start` takes the exec and a list of arguments
        self.process.start('ping',['127.0.0.1'])

    def initUI(self):
        # Layout are better for placing widgets
        layout = QtWidgets.QHBoxLayout()
        self.runButton = QtWidgets.QPushButton('Run')
        self.runButton.clicked.connect(self.callProgram)
        self.runButton1 = QtWidgets.QPushButton('End')
        self.runButton1.clicked.connect(self.close)

        self.output = QtWidgets.QTextEdit()

        layout.addWidget(self.output)
        layout.addWidget(self.runButton)
        layout.addWidget(self.runButton1)

        centralWidget = QtWidgets.QWidget()
        centralWidget.setLayout(layout)
        self.setCentralWidget(centralWidget)

        # QProcess object for external app
        self.process = QtCore.QProcess(self)
        # QProcess emits `readyRead` when there is data to be read
        self.process.readyRead.connect(self.dataReady)

        # Just to prevent accidentally running multiple times
        # Disable the button when process starts, and enable it when it finishes
        self.process.started.connect(lambda: self.runButton.setEnabled(False))
        self.process.finished.connect(lambda: self.runButton.setEnabled(True))


#Function Main Start
def main():
    app = QtWidgets.QApplication(sys.argv)
    ui=gui()
    ui.show()
    sys.exit(app.exec_())
#Function Main END

if __name__ == '__main__':
    main() 

回答1:


You must connect the clicked signal with the QProcess kill slot:

def initUI(self):
    [...]
    self.runButton1 = QtWidgets.QPushButton('End')
    # self.runButton1.clicked.connect(self.close)
    [...]

    # QProcess object for external app
    self.process = QtCore.QProcess(self)
    # QProcess emits `readyRead` when there is data to be read
    self.process.readyRead.connect(self.dataReady)
    self.runButton1.clicked.connect(self.process.kill)
    # Just to prevent accidentally running multiple times
    # Disable the button when process starts, and enable it when it finishes
    self.process.started.connect(lambda: self.runButton.setEnabled(False))
    self.process.finished.connect(lambda: self.runButton.setEnabled(True))


来源:https://stackoverflow.com/questions/46414272/how-to-stop-running-pyqt5-program-without-closing-the-gui-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!