打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
PyQt Qthread 输出到textBrowser
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtGui, QtCore
import sys, subprocess, time
class myThread(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)
         
    def run(self):
        self.pipe = subprocess.Popen('ping 127.0.0.1', stdout=subprocess.PIPE)
        while self.pipe.poll() == None:
            self.emit(QtCore.SIGNAL("getLog(QString)"), QtCore.QString.fromUtf8((self.pipe.stdout.readline().rstrip())))
            time.sleep(0.2)
        restInfo = self.pipe.stdout.readlines()
        if len(restInfo):
            for line in restInfo:
                self.emit(QtCore.SIGNAL("writeLog(QString)"), QtCore.QString.fromUtf8((line.rstrip())))
class MainWindow(object):
    def __init__(self, Dialog):
        self.bt = QtGui.QWidget(Dialog)
        Dialog.setMinimumWidth(400)
        Dialog.setMinimumHeight(300)
        self.button = QtGui.QPushButton(self.bt)
        self.button.setText('Start')
        self.button_2 = QtGui.QPushButton(self.bt)
        self.button_2.setText('Stop')
        self.text = QtGui.QPlainTextEdit(self.bt)
        self.text.setFont(QtGui.QFont('Courier New'10))
        self.verticalLayout = QtGui.QVBoxLayout(Dialog)
        self.verticalLayout.addWidget(self.button)
        self.verticalLayout.addWidget(self.button_2)
        self.verticalLayout.addWidget(self.text)
         
        self.th1 = myThread()
        QtCore.QObject.connect(self.th1, QtCore.SIGNAL("getLog(QString)"), self.writeLog)
        QtCore.QObject.connect(self.button, QtCore.SIGNAL('clicked()'), self.run)
        QtCore.QObject.connect(self.button_2, QtCore.SIGNAL('clicked()'), self.stop)
     
    def run(self):
        self.th1.start()
     
    def stop(self):
        self.th1.pipe.terminate()
     
    def writeLog(selfstr):
        self.text.appendPlainText(str)
     
app = QtGui.QApplication(sys.argv)
dlg = QtGui.QDialog()
main = MainWindow(dlg)       
dlg.show()
sys.exit(app.exec_())
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Python应用:使用PyQT制作视频播放器
如何在PyQt或PySide中更改最小化事件行为?
PyQt4 教程(5)
Python实现可以语音聊天的桌面宠物程序
PyQt4 信号和槽详解
PyQt5快速入门(六)PyQt5 GUI界面设计
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服