python怎么添加按钮_在Python QProcess示例中添加按钮和单独的窗口

我正在尝试使用QProcess并将标准输出读取到由按钮启动的QTextEdit.我该如何适应this example?我必须为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()

解决方法:

使用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()

标签:qprocess,pyqt,python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值