溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PyQt5中如何使用textBrowser實(shí)現(xiàn)顯示print輸出語(yǔ)句

發(fā)布時(shí)間:2020-11-09 15:57:38 來(lái)源:億速云 閱讀:5761 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

今天就跟大家聊聊有關(guān)PyQt5中如何使用textBrowser實(shí)現(xiàn)顯示print輸出語(yǔ)句,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

開(kāi)發(fā)python程序處理大數(shù)據(jù)量的時(shí)候,少不了使用print語(yǔ)句看看輸出結(jié)果;長(zhǎng)時(shí)間處理數(shù)據(jù)時(shí)用print輸出處理進(jìn)展情況。使用PyQt5開(kāi)發(fā)了UI界面后,本能地想讓已自己調(diào)試好的py代碼中的print輸出到UI的textBrowser中顯示出來(lái)。在CSDN上查了不少結(jié)果,一般都是使用多線程。我對(duì)多線程研究不多,就采用了變通辦法,效果還挺好。

在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()
    #.......
    #........中間自動(dòng)生成代碼省去....
    #........
    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")
    #..........其它語(yǔ)句

 def printf(self,mypstr):
   ###
   自定義類print函數(shù),借用c語(yǔ)言 printf 
   Mypstr:是待顯示的字符串
   ###
  self.textBrowser.append(mypstr)  #在指定的區(qū)域顯示提示信息
  self.cursor=self.tetxBrowser.textCursor()
  self.tetxBrowser.moveCursor(self.cursor.End) #光標(biāo)移到最后,這樣就會(huì)自動(dòng)顯示出來(lái)
  QtWidgets.QApplication.processEvents() #一定加上這個(gè)功能,不然有卡頓

其它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  #單獨(dú)調(diào)試成功代碼,可將正常print語(yǔ)句稍加改造


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 #點(diǎn)mouse調(diào)用改密碼功能。
    
  def chpsw_clicked(self, e):
    """
    change psw
    """
    print('change psw record')
 
  def _runget(self,ui):  #此處調(diào)用單獨(dú)開(kāi)發(fā)的py代碼。
    run_get(ui)  #是 runget.py 中主程序的入口方法。

  @pyqtSlot()
  def on_pushBut_get_clicked(self):
    """
    Slot documentation goes here.
    這是槽函數(shù),調(diào)用事先開(kāi)發(fā)好的模塊
    """
    # TODO: not implemented yet
    self.printf("\n自動(dòng)捕獲信息分析數(shù)據(jù),您等著瞧!")
    self._runget(ui)   #傳入ui實(shí)例是關(guān)鍵
  # ...........省略非相關(guān)代碼.....

if __name__ == "__main__":
#這是Ui界面主程序,注意ui這個(gè)實(shí)例化對(duì)象,就OK了
  app = QtWidgets.QApplication(sys.argv)
  app.aboutToQuit.connect(app.deleteLater)
  ui = MainWindow()
  ui.show()
  sys.exit(app.exec_())

run_get(ui)是單獨(dú)的調(diào)試成功的runget.py程序中的主入口,簡(jiǎn)化如下:

#!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
#..........
#......省略無(wú)關(guān)代碼....
#........
def run_get(ui):
  #1全局變量,并打開(kāi)設(shè)置
  glob_var_chrome() #
  #2.聯(lián)網(wǎng)
  if (login_nsso(gl_url,gl_user,gl_pass)!='OK'):
    #print("登錄系統(tǒng)不成功,無(wú)法進(jìn)行下去,檢查網(wǎng)絡(luò)正常后,可再運(yùn)行。") #這是正常的print代碼
    ui.printf("登錄系統(tǒng)不成功,無(wú)法進(jìn)行下去,檢查網(wǎng)絡(luò)正常后,可再運(yùn)行。"  #這是知適應(yīng)ui界面輸出的printf
    browser.quit()
    
     #............簡(jiǎn)化代碼.........
     #.....................

if __name__=='__main__':
  run_get()  #單獨(dú)運(yùn)行的調(diào)用時(shí)不用傳ui參數(shù), run_get(ui),是應(yīng)對(duì)UI界面來(lái)調(diào)用的。

看完上述內(nèi)容,你們對(duì)PyQt5中如何使用textBrowser實(shí)現(xiàn)顯示print輸出語(yǔ)句有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI