pyqt - 添加按钮和单独的窗口到 python QProcess示例

  显示原文与译文双语对照的内容
0 0

我试图使用 QProcess,并将stdout读到一个由按钮启动的QTextEdit 。 我如何适应这个例子这样做? 我必须为QProcess调用单独的类?

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
import sys
class MyQProcess(QProcess): 
 def __init__(self): 
 #Call base class method 
 QProcess.__init__(self)
 #Create an instance variable here (of type QTextEdit)
 self.edit = QTextEdit()
 self.edit.setWindowTitle("QTextEdit Standard Output Redirection")
 self.edit.show() 
 #Define Slot Here 
 @pyqtSlot()
 def readStdOutput(self):
 self.edit.append(QString(self.readAllStandardOutput()))
def main(): 
 app = QApplication(sys.argv)
 qProcess = MyQProcess()
 qProcess.setProcessChannelMode(QProcess.MergedChannels); 
 qProcess.start("ldconfig -v") 
 QObject.connect(qProcess,SIGNAL("readyReadStandardOutput()"),qProcess,SLOT("readStdOutput()"));
 return app.exec_()
if __name__ == '__main__':
 main()
时间: 原作者:

0 0

使用 QPushButton 制作按钮。

使用 QPushButton.clicked.connect 绑定事件。

例如:

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class MyWindow(QWidget):
 def __init__(self):
 QWidget.__init__(self)
 self.edit = QTextEdit()
 self.edit.setWindowTitle("QTextEdit Standard Output Redirection")
 self.button = QPushButton('Run ldconfig')
 self.button.clicked.connect(self.onClick)
 layout = QVBoxLayout(self)
 layout.addWidget(self.edit)
 layout.addWidget(self.button)
 @pyqtSlot()
 def readStdOutput(self):
 self.edit.append(QString(self.proc.readAllStandardOutput()))
 def onClick(self):
 self.proc = QProcess()
 self.proc.start("echo hello")
 self.proc.setProcessChannelMode(QProcess.MergedChannels);
 QObject.connect(self.proc, SIGNAL("readyReadStandardOutput()"), self, SLOT("readStdOutput()"));
def main():
 app = QApplication(sys.argv)
 win = MyWindow()
 win.show()
 return app.exec_()
if __name__ == '__main__':
 main()
原作者:
...