開發python程序處理大數據量的時候,少不了使用print語句看看輸出結果;長時間處理數據時用print輸出處理進展情況。使用PyQt5開發了UI界面后,本能地想讓已自己調試好的py代碼中的print輸出到UI的textBrowser中顯示出來。在CSDN上查了不少結果,一般都是使用多線程。我對多線程研究不多,就采用了變通辦法,效果還挺好。
在Ui界面程序(Ui_startaml.py)中設置textBrowser用于顯示程序輸出信息,并自己定義代碼(def printf ),以后將.py程序中凡是用print的地方改用ui.printf()調用就OK.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# -*- 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) 類呢,比如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# -*- 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程序中的主入口,簡化如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#!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內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_43097265/article/details/92830565