|
发表于 2024-3-29 16:13:02
|
显示全部楼层
对不起,我给出的回答中的代码截断了。以下是完整的代码示例:
- from PySide6.QtGui import QTextCharFormat, QColor
- from PySide6.QtCore import Qt
- def print_with_color(self, variable1, text1, variable2, text2, variable3, Text3, variable4, Text4, color1, color2, color3, color4, color5, color6, color7, color8):
- cursor = self.plainTextEdit.textCursor()
-
- format1 = QTextCharFormat()
- format1.setForeground(color1)
- format2 = QTextCharFormat()
- format2.setForeground(color2)
- format3 = QTextCharFormat()
- format3.setForeground(color3)
- format4 = QTextCharFormat()
- format4.setForeground(color4)
- format5 = QTextCharFormat()
- format5.setForeground(color5)
- format6 = QTextCharFormat()
- format6.setForeground(color6)
- format7 = QTextCharFormat()
- format7.setForeground(color7)
- format8 = QTextCharFormat()
- format8.setForeground(color8)
-
- cursor.insertText(variable1, format1)
- cursor.insertText(text1, format1)
- cursor.insertText(variable2, format2)
- cursor.insertText(text2, format2)
- cursor.insertText(variable3, format3)
- cursor.insertText(Text3, format3)
- cursor.insertText(variable4, format4)
- cursor.insertText(Text4, format4)
- # 可以根据需要继续插入其他变量和文本
-
- self.print_with_color("variable1", " Text1 ", "variable2", " text2", "variable3", "Text3", "variable4", "Text4", Qt.red, Qt.blue, Qt.green, Qt.black, Qt.yellow, Qt.magenta, Qt.cyan, Qt.darkGray)
复制代码
在这个例子中,我们使用了多个`QTextCharFormat`对象来设置不同段落的颜色。根据需要,您可以继续添加更多的变量和文本,并为它们指定相应的颜色。最后,您可以调用`self.print_with_color()`函数并传递相应的参数来打印带有不同颜色的文本。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|