詳解PyQt5中textBrowser顯示print語句輸出的簡單方法
阿新 • • 發佈:2020-08-10
開發python程式處理大資料量的時候,少不了使用print語句看看輸出結果;長時間處理資料時用print輸出處理進展情況。使用PyQt5開發了UI介面後,本能地想讓已自己除錯好的py程式碼中的print輸出到UI的textBrowser中顯示出來。在CSDN上查了不少結果,一般都是使用多執行緒。我對多執行緒研究不多,就採用了變通辦法,效果還挺好。
在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語句輸出的簡單方法的文章就介紹到這了,更多相關PyQt5 textBrowser顯示print內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!