Created
May 14, 2020 07:36
-
-
Save blockinhead/05391cb951fc1b86d58977363f4d625b to your computer and use it in GitHub Desktop.
qprocess example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| import sys | |
| from PySide2 import QtWidgets, QtCore | |
| PROGRAMS = [ | |
| ['ping.exe', ['google.com']], | |
| ['notepad.exe', []], | |
| ['nothing', []], | |
| ] | |
| class MyDialog(QtWidgets.QDialog): | |
| def __init__(self, parent=None): | |
| super(MyDialog, self).__init__(parent) | |
| layout = QtWidgets.QVBoxLayout() | |
| self.setLayout(layout) | |
| self.text_edit = QtWidgets.QTextEdit() | |
| layout.addWidget(self.text_edit) | |
| self.button = QtWidgets.QPushButton('press me!') | |
| self.button.pressed.connect(self.button_pressed) | |
| layout.addWidget(self.button) | |
| def button_pressed(self): | |
| self.button.setDisabled(True) | |
| process = QtCore.QProcess(self) | |
| process.errorOccurred.connect(self.on_error) | |
| process.readyReadStandardOutput.connect(self.receive_message) | |
| process.finished.connect(self.on_finished) | |
| process.start(*random.choice(PROGRAMS)) | |
| def on_error(self, err): | |
| process = self.sender() | |
| self.text_edit.append( | |
| 'process %s error %s' % (process.program(), str(err)) | |
| ) | |
| self.button.setDisabled(False) | |
| def on_finished(self): | |
| process = self.sender() | |
| self.text_edit.append('process %s finished' % process.program()) | |
| self.button.setDisabled(False) | |
| def receive_message(self): | |
| process = self.sender() | |
| self.text_edit.append( | |
| '%s' % str(process.readAllStandardOutput()).strip() | |
| ) | |
| if __name__ == '__main__': | |
| app = QtWidgets.QApplication(sys.argv) | |
| d = MyDialog() | |
| d.show() | |
| app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment