pyqt QProcess start 启动qt的带命令行的exe程序
时间: 2024-05-10 08:17:50 浏览: 166
可以使用 `QProcess` 类来启动带命令行的 `exe` 程序。下面是一个简单的例子:
```python
import sys
from PyQt5.QtCore import QProcess, QIODevice
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.process = QProcess(self)
self.process.setProcessChannelMode(QProcess.MergedChannels)
self.process.readyReadStandardOutput.connect(self.on_ready_read)
self.text_edit = QTextEdit(self)
self.text_edit.setReadOnly(True)
self.setCentralWidget(self.text_edit)
self.button = QPushButton('Start', self)
self.button.clicked.connect(self.on_button_clicked)
self.setCentralWidget(self.button)
def on_ready_read(self):
data = self.process.readAllStandardOutput().data().decode()
self.text_edit.moveCursor(QTextEdit.End)
self.text_edit.insertPlainText(data)
def on_button_clicked(self):
self.process.start('my_program.exe', ['-arg1', 'value1', '-arg2', 'value2'])
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
在这个例子中,`QProcess` 类被用来启动一个名为 `my_program.exe` 的程序,并传递一些参数。程序的输出被连接到 `readyReadStandardOutput` 信号,这样我们就可以在 `QTextEdit` 中显示它。`QPushButton` 被用来启动程序。
阅读全文
相关推荐

















