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

Posted

技术标签:

【中文标题】如何在 QTextBrowser (Html) 中设置文本颜色? (PyQt)【英文标题】:How do I set Text Colour in QTextBrowser (Html)?? (PyQt) 【发布时间】:2014-04-27 13:49:19 【问题描述】:

我正在尝试在创建的QTextBrowser 中为 html 文本设置字体颜色。我已经使用基本的 Html 命令来设置段落、更改字体大小等,但是在设置字体颜色时,它似乎不起作用?

我使用的代码如下所示:

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>"""
            )

它通过使用&lt;h1&gt; 使Key 成为标题(粗体和放大),但使用colour tags 或evem colour codes(例如#00ff00)似乎不起作用

【问题讨论】:

这是color 不是colour ;) en.wikipedia.org/wiki/Color color 有不同的拼写方式:D 【参考方案1】:

如 cmets 中所述,正确的属性名为 color 而不是 colour,考虑到这一点,我将完全取消 font 元素 as its long since deprecated 并将您的代码更改为,例如:

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>"""
            )

更好的是使用外部样式表将您的 CSS 移出内联,然后将一个类应用于 div。此外,所有元素都应位于body 标签内,因此您还应将h1 移至body 下方

考虑到这一点,我对QTextBrowser 并不熟悉。

【讨论】:

以上是关于如何在 QTextBrowser (Html) 中设置文本颜色? (PyQt)的主要内容,如果未能解决你的问题,请参考以下文章