开发python程序处理大数据量的时候,少不了使用print语句看看输出结果;长时间处理数据时用print输出处理进展情况。使用PyQt5开发了UI界面后,本能地想让已自己调试好的py代码中的print输出到UI的textBrowser中显示出来。一般都是使用多线程。我对多线程研究不多,就采用了变通办法,效果还挺好。

在Ui界面程序(Ui_startaml.py)中设置textBrowser用于显示程序输出信息,并自己定义代码(def printf ),以后将.py程序中凡是用print的地方改用ui.printf()调用就OK.

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'D:\aml\startaml.ui'
# Created by: PyQt5 UI code generator 5.11.3
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(490, 390)
MainWindow.setMaximumSize(QtCore.QSize(490, 390))
font = QtGui.QFont()
#.......
#........中间自动生成代码省去....
#........
self.textBrowser = QtWidgets.QTextBrowser(self.centralWidget)
self.textBrowser.setGeometry(QtCore.QRect(10, 109, 471, 221))
self.textBrowser.setMaximumSize(QtCore.QSize(16777215, 16777215))
font = QtGui.QFont()
font.setFamily("宋体")
self.textBrowser.setFont(font)
self.textBrowser.setObjectName("textBrowser")
#..........其它语句
def printf(self,mypstr):
###

自定义类print函数,借用c语言 printf

Mypstr:是待显示的字符串

###
self.textBrowser.append(mypstr) #在指定的区域显示提示信息
self.cursor=self.tetxBrowser.textCursor()
self.tetxBrowser.moveCursor(self.cursor.End) #光标移到最后,这样就会自动显示出来
QtWidgets.QApplication.processEvents() #一定加上这个功能,不然有卡顿
其它py程序如何去调用class Ui_MainWindow(object) 类呢,比如:
# -*- coding: utf-8 -*-
"""
Module implementing MainWindow.

这是ui界面主程序,它将调用已调试成功的.py程序。如runget.py

"""
from PyQt5 import QtWidgets
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow
from Ui_startaml import Ui_MainWindow
import sys
sys.path.append('src')
from runget import run_get #单独调试成功代码,可将正常print语句稍加改造
class MainWindow(QMainWindow, Ui_MainWindow):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor
@param parent reference to the parent widget
@type QWidget
"""
super(MainWindow,self).__init__(parent)
self.setupUi(self)
self.graphicsPsw.mousePressEvent=self.chpsw_clicked #点mouse调用改密码功能。
def chpsw_clicked(self, e):
"""
change psw
"""
print('change psw record')
def _runget(self,ui): #此处调用单独开发的py代码。
run_get(ui) #是 runget.py 中主程序的入口方法。
@pyqtSlot()
def on_pushBut_get_clicked(self):
"""
Slot documentation goes here.

这是槽函数,调用事先开发好的模块

"""
# TODO: not implemented yet
self.printf("\n自动捕获信息分析数据,您等着瞧!")
self._runget(ui) #传入ui实例是关键
# ...........省略非相关代码.....
if __name__ == "__main__":
#这是Ui界面主程序,注意ui这个实例化对象,就OK了
app = QtWidgets.QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
ui = MainWindow()
ui.show()
sys.exit(app.exec_())
run_get(ui)是单独的调试成功的runget.py程序中的主入口,简化如下:
#!C:\\Anaconda3\\python.exe
# -*- coding: utf-8 -*-
runget.py
"""
Created on Wed Mar 13 15:32:50 2019
@author: yuce_hz 2019年3月11日,runget.py
""""
import re
import os
import time
import requests
from requests.exceptions import RequestException
from lxml import etree
#..........
#......省略无关代码....
#........
def run_get(ui):
#1全局变量,并打开设置
glob_var_chrome() #
#2.联网
if (login_nsso(gl_url,gl_user,gl_pass)!='OK'):
#print("登录系统不成功,无法进行下去,检查网络正常后,可再运行。") #这是正常的print代码
ui.printf("登录系统不成功,无法进行下去,检查网络正常后,可再运行。" #这是知适应ui界面输出的printf
browser.quit()
#............简化代码.........
#.....................
if __name__=='__main__':
run_get() #单独运行的调用时不用传ui参数, run_get(ui),是应对UI界面来调用的。

到此这篇关于详解PyQt5中textBrowser显示print语句输出的简单方法的文章就介绍到这了