詳解PyQt5中textBrowser顯示print語句輸出的簡單方法
開發(fā)python程序處理大數(shù)據(jù)量的時候,少不了使用print語句看看輸出結(jié)果;長時間處理數(shù)據(jù)時用print輸出處理進展情況。使用PyQt5開發(fā)了UI界面后,本能地想讓已自己調(diào)試好的py代碼中的print輸出到UI的textBrowser中顯示出來。在CSDN上查了不少結(jié)果,一般都是使用多線程。我對多線程研究不多,就采用了變通辦法,效果還挺好。
在Ui界面程序(Ui_startaml.py)中設(shè)置textBrowser用于顯示程序輸出信息,并自己定義代碼(def printf ),以后將.py程序中凡是用print的地方改用ui.printf()調(diào)用就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函數(shù),借用c語言 printf Mypstr:是待顯示的字符串 ### self.textBrowser.append(mypstr) #在指定的區(qū)域顯示提示信息 self.cursor=self.tetxBrowser.textCursor() self.tetxBrowser.moveCursor(self.cursor.End) #光標(biāo)移到最后,這樣就會自動顯示出來 QtWidgets.QApplication.processEvents() #一定加上這個功能,不然有卡頓
其它py程序如何去調(diào)用class Ui_MainWindow(object) 類呢,比如:
# -*- coding: utf-8 -*- """ Module implementing MainWindow. 這是ui界面主程序,它將調(diào)用已調(diào)試成功的.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 #單獨調(diào)試成功代碼,可將正常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調(diào)用改密碼功能。 def chpsw_clicked(self, e): """ change psw """ print('change psw record') def _runget(self,ui): #此處調(diào)用單獨開發(fā)的py代碼。 run_get(ui) #是 runget.py 中主程序的入口方法。 @pyqtSlot() def on_pushBut_get_clicked(self): """ Slot documentation goes here. 這是槽函數(shù),調(diào)用事先開發(fā)好的模塊 """ # TODO: not implemented yet self.printf("\n自動捕獲信息分析數(shù)據(jù),您等著瞧!") self._runget(ui) #傳入ui實例是關(guān)鍵 # ...........省略非相關(guān)代碼..... 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)是單獨的調(diào)試成功的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 #.......... #......省略無關(guān)代碼.... #........ def run_get(ui): #1全局變量,并打開設(shè)置 glob_var_chrome() # #2.聯(lián)網(wǎng) if (login_nsso(gl_url,gl_user,gl_pass)!='OK'): #print("登錄系統(tǒng)不成功,無法進行下去,檢查網(wǎng)絡(luò)正常后,可再運行。") #這是正常的print代碼 ui.printf("登錄系統(tǒng)不成功,無法進行下去,檢查網(wǎng)絡(luò)正常后,可再運行。" #這是知適應(yīng)ui界面輸出的printf browser.quit() #............簡化代碼......... #..................... if __name__=='__main__': run_get() #單獨運行的調(diào)用時不用傳ui參數(shù), run_get(ui),是應(yīng)對UI界面來調(diào)用的。
到此這篇關(guān)于詳解PyQt5中textBrowser顯示print語句輸出的簡單方法的文章就介紹到這了,更多相關(guān)PyQt5 textBrowser顯示print內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決python3.6 右鍵沒有 Edit with IDLE的問題
這篇文章主要介紹了解決python3.6 右鍵沒有 Edit with IDLE的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03macOS M1(AppleSilicon) 安裝TensorFlow環(huán)境
蘋果為M1芯片的Mac提供了TensorFlow的支持,本文主要介紹了如何給使用M1芯片的macOS安裝TensorFlow的環(huán)境,感興趣的可以了解一下2021-08-08python3 使用函數(shù)求兩個數(shù)的和與差
這篇文章主要介紹了python3 使用函數(shù)求兩個數(shù)的和與差,具有很好的參考價值,希望對大家有所幫助。2021-05-05pyinstaller將含有多個py文件的python程序做成exe
這篇文章主要介紹了pyinstaller將含有多個py文件的python程序做成exe,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Python中pandas的dataframe過濾數(shù)據(jù)方法
這篇文章主要介紹了Python中pandas的dataframe過濾數(shù)據(jù)方法,Pandas是另外一個用于處理高級數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)分析的Python庫,Pandas是基于Numpy構(gòu)建的一種工具,需要的朋友可以參考下2023-07-07