【问题标题】:wxPython StyledTextCtrl Set (Partial) Text ColorwxPython StyledTextCtrl 设置(部分)文本颜色
【发布时间】:2012-05-10 13:36:34
【问题描述】:

如何在 StyledTextCtrl 中设置文本颜色,但只能设置一些单词?我的意思是假设我有

露西是蓝色的

我希望将“蓝色”这个词染成蓝色

【问题讨论】:

    标签: python wxpython wxwidgets textcolor wxstyledtextctrl


    【解决方案1】:

    有关 StyledTextCtrl,请参阅 wxPython 演示。它显示了如何做这件事。我认为您正在寻找的位是这样的:

    ed.StartStyling(190, 0xff)
    ed.SetStyling(20, 2)
    

    其中 190 是第 190 个字符,您可以为接下来的 20 个字符设置样式。

    【讨论】:

    • 您首先忘记了设置样式(示例中为 2 号):StyleSetSpec(2, "fore:#0000ff")
    • 嗯,这也在演示示例代码中。我只是没有注意到/包括它。
    • ed.StartStyling(190, 0xff) 中的 0xff 是什么?
    【解决方案2】:

    要改变一行的样式,你必须得到第一个字节和结束字节的位置。然后,您可以定义从第一个字节开始的样式 (StyleSetSpec) (StartStyling) 并应用于整行 (SetStyling)。您必须在结束字节处重新应用默认样式 (0)。这是我的代码:

    # Move to line
    self.editname.GotoLine(line-1)
    # Get position
    pos = self.editname.GetCurrentPos()
    # Define style 4
    self.editname.StyleSetSpec(4, "back:#ff0000")
    # Starts style at position pos
    self.editname.StartStyling(pos, 0xffff)
    # Until posend position, apply style 4
    self.editname.SetStyling(posend-pos, 4)
    # Restore style 0 after the ending byte of the line
    self.editname.SetStyling(posend, 0)
    

    【讨论】:

      【解决方案3】:

      在 text_area 是 StyledCtrlText 时使用它

      self.text_area.StyleSetSpec(stc.STC_P_DEFAULT,"fore:#FF0000")
      

      接下来放上你要改变颜色的文字

      【讨论】:

        猜你喜欢
        • 2012-06-07
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        • 2017-06-23
        • 2012-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多