Problem with 'unicode' object

Hi ,
I’m struggling with Colour. I want to change the color of certain words based on a list.
But I faced to error : “AttributeError: ‘unicode’ object has no attribute ‘SetForegroundColour’”
I know that the format maybe wrong , but don’t know how to change it :

def onChange(self, event):

    string1 = self.entry1.GetValue()
    string2 = ''
    words = string1.split()
    for j in range(len(words)):
        was = False
        for i in range(len(list)):
            if words[j] == list[i]:
                #string2 += ' ' + (words[j])
                string2 += ' ' + (words[j].SetForegroundColour(wx.Color(255, 0, 0)))
                was = True
        if was == False:
            string2 += ' ' + words[j]
    self.display.SetValue(string2)

Any idea ?

Thanks

Test.py (1.98 KB)

You get that error because you are trying to call SetForegroundColour on a python string object – that is never going to work, you could only call a method like that on a ax object.

···

I don’t think a regular text entry object supports multiple colors-- you will need to use a widget that does – maybe text control or rich text control.

But in any case, you will be making a call on the ax object that holds the text – not the text itself.

CHB

Sent from my iPhone

On Nov 9, 2016, at 7:57 AM, mani mana864@gmail.com wrote:

Hi ,
I’m struggling with Colour. I want to change the color of certain words based on a list.
But I faced to error : “AttributeError: ‘unicode’ object has no attribute ‘SetForegroundColour’”
I know that the format maybe wrong , but don’t know how to change it :

def onChange(self, event):

    string1 = self.entry1.GetValue()
    string2 = ''
    words = string1.split()
    for j in range(len(words)):
        was = False
        for i in range(len(list)):
            if words[j] == list[i]:
                #string2 += ' ' + (words[j])
                string2 += ' ' + (words[j].SetForegroundColour(wx.Color(255, 0, 0)))
                was = True
        if was == False:
            string2 += ' ' + words[j]
    self.display.SetValue(string2)

Any idea ?

Thanks

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

<Test.py>

As Chris said, while I was composing this reply, a unicode string doesn’t have any way of specifying how it is displayed, only the object that is presenting the string can do that. In your case self.display is a wx.TextCtrl and I believe you can specify how different ranges of the whole text string are displayed using its SetStyle method to use a wx.TextAttr to set the attributes of the each specified range. You can do similar things with a wx.RichTextCtrl plus use BeginTextColour and EndTextColour as you build the text to be displayed. See the code for the RichTextCtrl in the wxPython Demo.

David Hughes
Forestfield Software

···

On Wednesday, 9 November 2016 15:57:23 UTC, mani wrote:

I’m struggling with Colour. I want to change the color of certain words based on a list.
But I faced to error : “AttributeError: ‘unicode’ object has no attribute ‘SetForegroundColour’”
I know that the format maybe wrong , but don’t know how to change it :