【问题标题】:How can I change the color of specific words in wxPython TextCtrl?如何更改 wxPython TextCtrl 中特定单词的颜色?
【发布时间】:2018-03-01 00:29:00
【问题描述】:

我创建了一个只显示引用的 gui。如何将下面引用中的所有单词“你”的颜色更改为蓝色?我试过 SetForegroundColour 但它会将整个文本变为蓝色。

代码如下:

import wx

string='''"Have more than thou showest,

Speak less than thou knowest,

Lend less than thou owest,

Ride more than thou goest,

Learn more than thou trowest,

Set less than thou throwest."

—The Fool in King Lear'''

class Quote(wx.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.InitUI()

    def InitUI(self):
        panel=wx.Panel(self)

        self.text=wx.TextCtrl(panel, pos=(20,20), size=(250,220),
            style=wx.TE_MULTILINE|wx.TE_READONLY)
        self.text.AppendText(string)
        self.text.SetForegroundColour("blue")

        self.SetSize(300,300)
        self.Centre()
        self.Show(True)

def main():
    app=wx.App()
    Quote(None)
    app.MainLoop()

if __name__ == '__main__':
    main()

Here's how it looks like

【问题讨论】:

    标签: text format wxpython textctrl


    【解决方案1】:

    您可能需要阅读RichTextCtrl https://wxpython.org/Phoenix/docs/html/richtextctrl_overview.html

    只需使用TextCtrl,您就可以
    1事后设置样式属性(感谢 Robin 的评论)或
    2将样式属性应用于你去的文本。

    事后设置样式属性,使用re 预先查找所有出现的单词:

    import wx
    import re
    
    string='''"Have more than thou showest,
    
    Speak less than thou knowest,
    
    Lend less than thou owest,
    
    Ride more than thou goest,
    
    Learn more than thou trowest,
    
    Set less than thou throwest."
    
    -The Fool in King Lear'''
    
    class Quote(wx.Frame):
        def __init__(self, *args, **kwargs):
            wx.Frame.__init__(self, None, -1)
            self.InitUI()
    
        def InitUI(self):
            panel=wx.Panel(self)
            word = 'thou'
            word_colour = wx.TextAttr(wx.BLUE)
            word_occurs = self.find_str(word,string)
            self.text=wx.TextCtrl(panel, pos=(20,20), size=(250,220),
                style=wx.TE_MULTILINE|wx.TE_READONLY)
            self.text.AppendText(string)
            for i in word_occurs:
                #SetStyle(start pos, end pos, style)
                self.text.SetStyle(i,i+len(word),word_colour)
            self.SetSize((300,300))
            self.Centre()
            self.Show(True)
    
        def find_str(self,sub,sent): #return positions of the word
            return [x.start() for x in re.finditer(sub,sent)]
    
    def main():
        app=wx.App()
        Quote()
        app.MainLoop()
    
    if __name__ == '__main__':
        main()
    

    执行此操作的冗长方法,即应用样式属性:

    import wx
    
    string1='''"Have more than'''
    string2='''showest,
    
    Speak less than'''
    string3='''knowest,
    
    Lend less than'''
    string4='''owest,
    
    Ride more than'''
    string5='''goest,
    
    Learn more than'''
    string6='''trowest,
    
    Set less than'''
    string7='''throwest."
    
    -The Fool in King Lear'''
    
    class Quote(wx.Frame):
        def __init__(self, *args, **kwargs):
            wx.Frame.__init__(self, None, -1)
            self.InitUI()
    
        def InitUI(self):
            panel=wx.Panel(self)
    
            self.text=wx.TextCtrl(panel, pos=(20,20), size=(250,220),
                style=wx.TE_MULTILINE|wx.TE_READONLY)
            self.text.AppendText(string1)
            self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
            self.text.AppendText(" thou ")
            self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
            self.text.AppendText(string2)
            self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
            self.text.AppendText(" thou ")
            self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
            self.text.AppendText(string3)
            self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
            self.text.AppendText(" thou ")
            self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
            self.text.AppendText(string4)
            self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
            self.text.AppendText(" thou ")
            self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
            self.text.AppendText(string5)
            self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
            self.text.AppendText(" thou ")
            self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
            self.text.AppendText(string6)
            self.text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
            self.text.AppendText(" thou ")
            self.text.SetDefaultStyle(wx.TextAttr(wx.NullColour))
            self.text.AppendText(string7)
    
            self.SetSize((300,300))
            self.Centre()
            self.Show(True)
    
    def main():
        app=wx.App()
        Quote()
        app.MainLoop()
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

    • 您还可以使用SetStyle 更改现有文本范围的样式。因此,您可以一步将整个字符串放入控件中,然后在适当的开始/结束位置设置样式。
    • @RobinDunn 我通过提问学到的回答问题比以往任何时候都多! :)
    • 谢谢,萨克森的罗尔夫。你的回答很有用。
    猜你喜欢
    • 2013-08-28
    • 2013-05-12
    • 1970-01-01
    • 2013-01-02
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2013-11-03
    相关资源
    最近更新 更多