繁体   English   中英

Python - pyqt5 - 将文本设置为具有不同颜色的 qtextbrowser

[英]Python - pyqt5 - Set text to qtextbrowser with different colors

我有一个字符串数组,其中包含正确拼写的单词和拼写错误的单词。 我想将所有这些单词设置为qtextbrowser并且我想让拼写错误的单词变成红色

wordlist = ['correct1', 'correct2', 'incorrect1', 'correct3', 'incorrect2']

您可以将拼写错误的单词包装在 html 标签中,并将内联样式设置为显示红色。

def check_misspelled(self, word):
    if ...:  # check if word is misspelled here
        word = '<span style=\" color: #ff0000;\">%s</span>' % word
    self.text_browser.append(word)
cursor = self.textBrowser.textCursor()
cursor.insertHtml('''<p><span style="color: red;">{} </span>'''.format(word))

这将解决“每次都附加到换行符”的问题。

暂无
暂无

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

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