简体   繁体   English

如何在QTextBrowser(Html)中设置文本颜色? (PyQt的)

[英]How do I set Text Colour in QTextBrowser (Html)?? (PyQt)

I am trying to set a font colour for Html text in a created QTextBrowser . 我正在尝试在创建的QTextBrowser为HTML文本设置字体颜色。 I've used basic Html Commands to set paragraphs, change font size etc. but when it comes to setting font colour, it doesn't seem to work? 我已经使用了基本的HTML命令来设置段落,更改字体大小等,但是在设置字体颜色时,它似乎不起作用吗?

The code that I used is shown below: 我使用的代码如下所示:

self.key = QtGui.QTextBrowser(self)
        self.key.setHtml(
            """<h1>Key</h1>
            <body>
            <font colour="red">
            GREEN = Overall Progress is 80% or above
            YELLOW = Overall Progress between 65%-79%
            Orange = Overall Progress is 64% or below
            </font>
            </body>"""
            )

It makes Key a header (Bold and Enlarged) via the use of <h1> but using colour tags or evem colour codes (eg #00ff00 ) don't seem to work 通过使用<h1>它使Key成为标头(粗体和放大),但使用colour tags或evem colour codes (例如#00ff00 )似乎不起作用

As noted in the comments, the correct property is named color not colour , with that in mind, I'd do away with the font element completely, as its long since deprecated and change your code to, eg: 如评论中所述,正确的属性命名为color not colour ,考虑到这一点,我将彻底放弃font元素, 因为它已被弃用很久了 ,因此将代码更改为例如:

self.key = QtGui.QTextBrowser(self)
        self.key.setHtml(
            """<body>
            <h1>Key</h1>
            <div style='color:red;'>
            GREEN = Overall Progress is 80% or above
            YELLOW = Overall Progress between 65%-79%
            Orange = Overall Progress is 64% or below
            </div>
            </body>"""
            )

Even better would be to use an external stylesheet to move your CSS out of being inline, and then apply a class to the div . 更好的方法是使用外部样式表将CSS移出内联,然后将一个类应用于div In addition, all elements should reside within the body tags, so you should also move your h1 below body 此外,所有元素都应位于body标签内,因此您还应将h1移到body以下

With that in mind, I'm not familiar with QTextBrowser however. 考虑到这一点,我对QTextBrowser并不熟悉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM