如何更改 wxPython StyleTextCtrl 中特定单词的颜色?

问题描述

在这里,我尝试使用 wxpython 中的 styletextctrl 为特定单词着色。但是单词的颜色不是保持不变的。我怎么解决这个问题。我想为我自己的编程语言文本突出显示

我不想改变整个文本字符串的颜色。它必须是特定的词。

这是我的代码

self.text_control = stc.StyledTextCtrl(self,style=wx.TE_MULTILINE | wx.TE_WORDWRAP)

self.font = wx.Font(10,wx.FONTFAMILY_DEFAULT,wx.FONTSTYLE_norMAL,wx.FONTWEIGHT_norMAL,True)

self.font = wx.Font(10,True)

self.text_control.SetViewWhiteSpace(False)
self.text_control.SetMargins(5,0)
self.text_control.SetMarginType(1,stc.STC_MARGIN_NUMBER)
self.text_control.SetMarginWidth(1,self.leftMargin)

self.text_control.StyleSetSpec(stc.STC_STYLE_LINENUMBER,'fore:#000000,back:#e8e8e8')

self.text_control.StyleSetFont(1,self.font)

text = self.text_control
code = text.Text
code = code.split(' ')

for i in code:

if filetype == "":

                if i == "import":
                    text.StyleSetSpec(wx.TE_HT_ON_TEXT,'fore:#235644')
                    #text.SetDefaultStyle(wx.TEXT_ATTR_TEXT_COLOUR(wx.BLUE))
                    #text.SetBackgroundColour(wx.BLUE)

                else:
                    text.StyleSetSpec(wx.TE_HT_ON_TEXT,'fore:#111111')
                    #text.SetDefaultStyle(wx.TEXT_ATTR_TEXT_COLOUR(wx.NullColour))
                    #text.SetBackgroundColour(wx.NullColour)


            elif filetype == "Python File":
                text.StyleSetSpec(wx.TEXT_ATTR_EFFECT_SHADOW,'fore:#235644,back:#e8e8e8')

            elif filetype == "C++ File":
                pass

            else:
                pass;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)