如果你已经有了self.plainTextEdit控件,并且通过重定向输出到其中,那么你可以在输出内容时使用QTextCursor来设置颜色。下面是一个示例函数,可以将notice以指定颜色显示在self.plainTextEdit中:
- def print_with_color(self, text, color):
- cursor = self.plainTextEdit.textCursor()
- format = QTextCharFormat()
-
- if color == "red":
- format.setForeground(Qt.red)
- elif color == "yellow":
- format.setForeground(Qt.yellow)
- elif color == "white":
- format.setForeground(Qt.white)
-
- cursor.insertText(text, format)
复制代码
然后,你可以在需要打印notice的地方调用这个函数,并传入相应的颜色参数。例如:
- notice = "Hello World!"
- self.print_with_color(notice, "red")
复制代码
上述代码会将notice以红色显示在self.plainTextEdit中。如果想要显示为其他颜色,只需要将颜色参数改为"yellow"或"white"即可。
希望能帮助到你!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。