在QTextEdit中改变单个字符串的颜色

11 投票
5 回答
23449 浏览
提问于 2025-04-18 10:13

我正在用PyQt和Qt4开发一个图形用户界面(GUI)。在我的界面里,有一个QTextEdit控件,可以写入各种数据。我想知道有没有办法改变QTextEdit中某个单词的颜色。

比如说:

redText = "I want this text red"
self.myTextEdit.write(redText)
blackText = "And this text black"
self.myTextEdit.append(blackText)

这可能吗?如果可以的话,我该怎么做呢?

祝好,

sudo!!

5 个回答

0

PySide和PyQt非常相似,所以如果有人在找PySide的相关内容,这段代码在PySide6中也能正常使用。

red_text = "I want this text red"
self.myTextEdit.setHtml(f"<span style=\"color:#ff0000;\" > {red_text} </span>")
black_text = "I want this text black"
self.myTextEdit.setHtml(f"<span style=\"color:#000000;\" > {black_text} </span>")
0

我也遇到过同样的问题,但一直没找到一个明确的解决办法。基本上,我的图形用户界面(GUI)在我弄清楚如何给文本上色之前,颜色重叠在一起,导致无法独立处理不同颜色的文本。

所以,有一天我在网上浏览的时候,收集了一些信息,发现了一些东西,比如:

        #Import QColor, this will be responsible for doing the job.
        from PyQt5.QtGui import QColor
        from PyQt5 import uic, QtWidgets

        class Program:
            def writeonthescreen(self):
                #Set a color
                Screen.your_text_edit.setTextColor(QColor(255, 51, 0))
                
                #Write colored text
                Screen.your_text_edit.append('Red')

                Screen.your_text_edit.setTextColor(QColor(0, 204, 0))
                        
                Screen.your_text_edit.append('Green')

                Screen.your_tex_edit.setTextColor(QColor(0, 0, 255))
                        
                Screen.your_text_edit.append('Blue')


        if __name__ == '__main__':
            '''
            "Screen" is the name we will use to name the screen to be loaded.
             Imagine that this screen contains a QTextEdit, and a button that when pressed displays your text.

            '''
            app = QtWidgets.QApplication([])
            Screen = uic.loadUi('./your_screen_path')
            Screen.button_showtext_on_the_screen.clicked.connect(Program.writeonthescreen)
            
            Screen.show()
            app.exec()
1

Nejat的回答对我有用,他把“.append()”换成了“+=”。

redText = "<span style=\" font-size:8pt; font-weight:600; color:#ff0000;\" >"
redText += "I want this text red"
redText += "</span>"
self.myTextEdit.write(redText)

blackText = "<span style=\" font-size:8pt; font-weight:600; color:#000000;\" >"
blackText += "And this text black")
blackText += "</span>"
self.myTextEdit.append(blackText)
8

经过一些研究其他人使用的方法,我找到了办法,想和大家分享一下。

我尝试了在QTextEdit中使用“.setHtml”这个功能,但没有成功。

我发现你可以先改变文本颜色,添加你的文本,然后再改变颜色,之后添加的任何文本都会变成你设置的颜色,但之前的文本颜色不会改变。

这里有个例子。

redColor = QColor(255, 0, 0)
blackColor = QColor(0, 0, 0)

# First, set the text color to red    
self.myTextEdit.setTextColor(redColor)

redText = "I want this text red"
self.myTextEdit.write(redText)

# To change it back to black, we manually use `setTextColor` again
self.myTextEdit.setTextColor(blackColor)

blackText = "And this text black"
self.myTextEdit.append(blackText)

另外,我还想补充一下,“.write”和“.append”这两个功能在我的“QTextEdit”类中不管用。不知道你的是否可以,但对我来说,“.insertPlainText”这个功能是有效的。只需要把你的字符串转换成“QString”,像这样:

blackText = QString(blackText)
12

你应该为它提供丰富的文本。这可以通过创建一个 <span> 标签,并将 color 属性设置为一个 RGB 值来实现:

redText = "<span style=\" font-size:8pt; font-weight:600; color:#ff0000;\" >"
redText.append("I want this text red")
redText.append("</span>")
self.myTextEdit.write(redText)

blackText = "<span style=\" font-size:8pt; font-weight:600; color:#000000;\" >"
blackText.append("And this text black")
blackText.append("</span>")
self.myTextEdit.append(blackText)

撰写回答