鱼C论坛

 找回密码
 立即注册
123
返回列表 发新帖
楼主: 凊风

字体颜色设置求教~

[复制链接]
 楼主| 发表于 2024-3-26 10:15:37 | 显示全部楼层
FishC 发表于 2024-3-26 10:14
下面是根据您的要求修改后的Python代码示例:from PySide6.QtGui import QTextCharFormat, QColorfrom  ...

回复请注意Python格式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-26 10:16:51 | 显示全部楼层
凊风 发表于 2024-03-26 10:15
回复请注意Python格式


非常抱歉,以下是经过修正的Python代码示例:
from PySide6.QtGui import QTextCharFormat, QColor
from PySide6.QtCore import Qt

def print_with_color(self, text1, variable1, text2, variable2, text3, variable3, text4, color1, color2, color3, color4, color5, color6, color7):
    cursor = self.plainTextEdit.textCursor()
    
    # 设置文本1的颜色
    format1 = QTextCharFormat()
    format1.setForeground(QColor(color1))
    cursor.insertText(text1, format1)
    
    # 设置变量1的颜色
    format2 = QTextCharFormat()
    format2.setForeground(QColor(color2))
    cursor.insertText(str(variable1), format2)
    
    # 设置文本2的颜色
    format3 = QTextCharFormat()
    format3.setForeground(QColor(color3))
    cursor.insertText(text2, format3)
    
    # 设置变量2的颜色
    format4 = QTextCharFormat()
    format4.setForeground(QColor(color4))
    cursor.insertText(str(variable2), format4)
    
    # 设置文本3的颜色
    format5 = QTextCharFormat()
    format5.setForeground(QColor(color5))
    cursor.insertText(text3, format5)
    
    # 设置变量3的颜色
    format6 = QTextCharFormat()
    format6.setForeground(QColor(color6))
    cursor.insertText(str(variable3), format6)
    
    # 设置文本4的颜色
    format7 = QTextCharFormat()
    format7.setForeground(QColor(color7))
    cursor.insertText(text4, format7)

调用函数示例:
self.print_with_color("Text1:", variable1, "Text2:", variable2, "Text3", variable3, "Text4:", "red", "blue", "green", "yellow", "orange", "purple", "pink")

非常抱歉之前的回复未按照Python的格式要求进行编写,希望这次的回答符合您的需求。如果您还有任何问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-26 10:19:46 | 显示全部楼层
FishC 发表于 2024-3-26 10:16
非常抱歉,以下是经过修正的Python代码示例:

再请问一下:PySide6中,QtextEdit的字体颜色又如何设置呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-26 10:20:01 | 显示全部楼层
凊风 发表于 2024-03-26 10:19
再请问一下:PySide6中,QtextEdit的字体颜色又如何设 ...


在PySide6中,可以使用QTextEdit控件的文本属性来设置字体颜色。下面是一个示例代码来演示如何设置QTextEdit的字体颜色:
from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit
from PySide6.QtGui import QTextCharFormat, QColor, QTextCursor

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.text_edit = QTextEdit()
        self.setCentralWidget(self.text_edit)

        self.set_text_color('Hello, world!', 'red')

    def set_text_color(self, text, color):
        format = QTextCharFormat()
        format.setForeground(QColor(color))

        cursor = self.text_edit.textCursor()
        cursor.insertText(text, format)

if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

在这个示例中,我们创建了一个继承自QMainWindow的窗口类MainWindow。在构造函数中,我们创建了一个QTextEdit控件并将其设置为主窗口的中央部件。然后,我们定义了一个名为set_text_color的方法,它接受要设置的文本和颜色作为参数。

在set_text_color方法中,我们首先创建一个QTextCharFormat对象,并使用setForeground方法设置其前景色为指定的颜色。然后,我们获取QTextEdit的文本光标,并使用insertText方法插入文本,并传递先前创建的QTextCharFormat对象。这样就可以将指定文本设置为指定颜色的字体了。

最后,我们创建了应用程序并显示了主窗口。

希望这个示例能帮助您理解如何在PySide6中设置QTextEdit控件的字体颜色。如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-29 16:09:31 | 显示全部楼层
凊风 发表于 2024-3-26 10:14
如果要打印print(Text1, variable1, text2, variable2,Text3,variable3,Text4)

其中 variable1、  ...


如果要打印print(variable1, Text1, variable2, text2, variable3,Text3,variable4,Text4)

其中 variable1、 variable2和variable3, variable4是变量。

那def print_with_color(self, variable1,  text1, variable2, text2,  variable3,Text3, variable4,Text4,color1, color2, color3,color4, color5,color6, color7, color8):这样的句式又要如何实现呢?

回复请注意Python格式。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-29 16:09:46 | 显示全部楼层
凊风 发表于 2024-03-29 16:09
如果要打印print(variable1, Text1, variable2, text ...


下面是根据您的要求修改后的Python代码示例:from PySide6.QtGui import QTextCharFormat, QColorfrom PySide6.QtCore import Qtdef print_with_color(self, variable1, text1, variable2, text2, variable3, Text3, variable4, Text4, color1, color2, color3, color4, color5, color

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-29 16:11:04 | 显示全部楼层
FishC 发表于 2024-3-29 16:09
下面是根据您的要求修改后的Python代码示例:from PySide6.QtGui import QTextCharFormat, QColorfrom  ...

你这回复好像卡断了。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-29 16:12:53 | 显示全部楼层
FishC 发表于 2024-3-29 16:09
下面是根据您的要求修改后的Python代码示例:from PySide6.QtGui import QTextCharFormat, QColorfrom  ...

你是不是卡了?  回复没结束啊?


回复请注意Python格式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-29 16:13:02 | 显示全部楼层
凊风 发表于 2024-03-29 16:12
你是不是卡了?  回复没结束啊?


回复请注意Pyth ...


对不起,我给出的回答中的代码截断了。以下是完整的代码示例:
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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-29 16:13:28 | 显示全部楼层
凊风 发表于 2024-03-29 16:11
你这回复好像卡断了。。。


抱歉,之前的回答似乎出现了问题。下面是修正后的代码示例:
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()
    format = QTextCharFormat()

    # 设置颜色
    if color1 == "red":
        format.setForeground(Qt.red)
    elif color1 == "blue":
        format.setForeground(Qt.blue)
    # 其他颜色设置...

    # 插入文本和变量
    cursor.insertText(variable1, format)
    cursor.insertText(text1)

    format.setForeground(QColor(color2))
    cursor.insertText(variable2, format)
    cursor.insertText(text2)

    format.setForeground(QColor(color3))
    cursor.insertText(variable3, format)
    cursor.insertText(Text3)

    format.setForeground(QColor(color4))
    cursor.insertText(variable4, format)
    cursor.insertText(Text4)

# 调用函数
self.print_with_color("Variable1", "Text1", "Variable2", "Text2", "Variable3", "Text3", "Variable4", "Text4", "red", "blue", "green", "purple", "orange", "yellow", "pink", "brown")

在这个例子中,根据传递的颜色参数,我们使用`QColor()`函数来创建颜色对象,并将其作为参数传递给`setForeground()`方法来设置文字的颜色。然后,我们使用`textCursor()`方法获取光标对象,并使用`insertText()`方法插入文本和变量。

您可以根据需要自由调整传递给`self.print_with_color()`函数的参数,并为每个文本片段和变量选择不同的颜色。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 12:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表