| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1255 人关注过本帖
标题:关于控制台定向输出到GUI时文字颜色字体的控制问题(大致说个方向就行)
只看楼主 加入收藏
蓝书
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2020-3-18
收藏
 问题点数:0 回复次数:1 
关于控制台定向输出到GUI时文字颜色字体的控制问题(大致说个方向就行)
我将控制台结果定向输出到UI界面,文字颜色字体怎么也改不了,请高手指点下。
以下代码源自互联网,用来方便交流用:我想把 输出结果:running hello done 字号变大,换个颜色
程序代码:
import sys
import time
from PyQt5.QtCore import QObject, pyqtSignal, QEventLoop, QTimer
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QTextEdit
from PyQt5.QtGui import QTextCursor

 
'''
控制台输出定向到Qtextedit中
'''

 

 
class Stream(QObject):
    """Redirects console output to text widget."""
    newText = pyqtSignal(str)

 
    def write(self, text):
        self.newText.emit(str(text))

 

 
class GenMast(QMainWindow):
    """Main application window."""
    def __init__(self):
        super().__init__()

 
        self.initUI()

 
        # Custom output stream.
        sys.stdout = Stream(newText=self.onUpdateText)

 
    def onUpdateText(self, text):
        """Write console output to text widget."""
        cursor = self.process.textCursor()
        cursor.movePosition(QTextCursor.End)
        cursor.insertText(text)
        self.process.setTextCursor(cursor)
        self.process.ensureCursorVisible()

 
    def closeEvent(self, event):
        """Shuts down application on close."""
        # Return stdout to defaults.
        sys.stdout = sys.__stdout__
        super().closeEvent(event)

 
    def initUI(self):
        """Creates UI window on launch."""
        # Button for generating the master list.
        btnGenMast = QPushButton('Run', self)
        btnGenMast.move(450, 50)
        btnGenMast.resize(100, 200)
        btnGenMast.clicked.connect(self.genMastClicked)

 
        # Create the text output widget.
        self.process = QTextEdit(self, readOnly=True)
        self.process.ensureCursorVisible()
        self.process.setLineWrapColumnOrWidth(500)
        self.process.setLineWrapMode(QTextEdit.FixedPixelWidth)
        self.process.setFixedWidth(400)
        self.process.setFixedHeight(200)
        self.process.move(30, 50)

 
        # Set window size and title, then show the window.
        self.setGeometry(300, 300, 600, 300)
        self.setWindowTitle('Generate Master')
        self.show()

 
    def printhello(self):
        print('hello')

 
    def genMastClicked(self):
        """Runs the main function."""
        print('Running...')
        self.printhello()
        loop = QEventLoop()
        QTimer.singleShot(2000, loop.quit)
        loop.exec_()
        print('Done.')

 

 
if __name__ == '__main__':
    # Run the application.
    app = QApplication(sys.argv)
    app.aboutToQuit.connect(app.deleteLater)
    gui = GenMast()
    sys.exit(app.exec_())
搜索更多相关主题的帖子: 控制台 输出 process text def 
2020-05-02 16:12
蓝书
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2020-3-18
收藏
得分:0 
抱歉,是我用QT时对输出框做过限定导致无法更改。
2020-05-02 17:05
快速回复:关于控制台定向输出到GUI时文字颜色字体的控制问题(大致说个方向就行 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018056 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved