pyQT5实现:在mywindow主窗口弹出windows的cmd窗口
时间: 2023-12-04 14:04:29 浏览: 163
Python 中PyQt5 点击主窗口弹出另一个窗口的实现方法
可以通过使用QProcess类来实现在mywindow主窗口弹出cmd窗口。具体实现方法可以参考以下代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction
from PyQt5.QtCore import QProcess
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)
cmdAction = QAction('Open CMD', self)
cmdAction.triggered.connect(self.openCMD)
self.toolbar = self.addToolBar('Open CMD')
self.toolbar.addAction(cmdAction)
self.setGeometry(300, 300, 350, 250)
self.setWindowTitle('My Window')
self.show()
def openCMD(self):
process = QProcess(self)
process.start('cmd.exe')
process.readyReadStandardOutput.connect(self.onReadyReadStandardOutput)
process.readyReadStandardError.connect(self.onReadyReadStandardError)
def onReadyReadStandardOutput(self):
process = self.sender()
output = process.readAllStandardOutput().data().decode()
self.textEdit.insertPlainText(output)
def onReadyReadStandardError(self):
process = self.sender()
output = process.readAllStandardError().data().decode()
self.textEdit.insertPlainText(output)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
sys.exit(app.exec_())
```
当点击工具栏中的“Open CMD”按钮时,会在mywindow主窗口中弹出cmd窗口。注意,在Windows系统中需要使用cmd.exe命令来打开命令行窗口。
阅读全文