richtext in wx3.0.1.1 error: 'module' object has no attribute 'TextAttrEx'

I upgraded to wxPython 3.0.1.1 and it seems to have broken my use of wx.RichTextCtrl. When I run this section of code:

def OnFont(self, evt):

    if not self.rtc.HasSelection():
        return

    r = self.rtc.GetSelectionRange()
    fontData = wx.FontData()
    fontData.EnableEffects(False)
    attr = rt.TextAttrEx()

I stops here with this error:

AttributeError: 'module' object has no attribute 'TextAttrEx'

If I use 2.8.12.1 (msw-unicode), I don’t get this error. If I search the source code of the wx.richtext module in the 2.8.12, I indeed find the TextAttrEx class. It is not in the wx3.0.1.1 code as far as I can tell.

Has it been renamed? I can’t find what it might be now by searching, either in the file or online.

Thanks,
Che

1 Like

Hi Che,

···

On 1/21/2015 17:36, C M wrote:

I upgraded to wxPython 3.0.1.1 and it seems to have broken my use of wx.RichTextCtrl. When I run this section of code:

def OnFont(self, evt):

        if not self.rtc.HasSelection():
            return

        r = self.rtc.GetSelectionRange()
        fontData = wx.FontData()
        fontData.EnableEffects(False)
        attr = rt.TextAttrEx()

I stops here with this error:

    AttributeError: 'module' object has no attribute 'TextAttrEx'

looks like this is a 2.9.0.1 change:
http://www.wxpython.org/recentchanges.php

Looks like it didn't get updated everywhere, as e.g. the Phoenix doc still shows it:
http://wxpython.org/Phoenix/docs/html/richtextctrl_overview.html?highlight=textattrex

Werner

OK, I just found this on the recent changes circa wx 2.9.01 (I had upgraded
from 2.8 so wasn't aware of this change) [thanks, Werner, I see you also
just found that for me as well]:

"wx.richtext.RichTextCtrl and related classes were refactored such that the
RTC uses the same attributes object as wx.TextCtrl. This means that instead
of using wx.richtext.RichtextAttr or TextAttrEx you'll just use wx.TextAttr
instead. Also, all of the flags and styles related to the text attributes
have been moved out of the wx.richtext module and into the main wx
namespace. Finally, wx.TextCtrl and RichTextCtrl now share some common base
classes."

So based on that, I changed the line to use wx.TextAttr, and it got
further. The next error was this:

Traceback (most recent call last):
  File "C:\Users\project\richTextPanel.py", line 547, in OnFont
    attr.SetFlags(rt.TEXT_ATTR_FONT)
AttributeError: 'module' object has no attribute 'TEXT_ATTR_FONT'

So, based on the text in that Recent Changes note (" Also, all of the flags
and styles related to the text attributes have been moved out of the
wx.richtext module and into the main wx namespace"), I changed this line:

    attr.SetFlags(rt.TEXT_ATTR_FONT)

to this:

     attr.SetFlags(wx.TEXT_ATTR_FONT)

and it got further.

However, now I am getting another error, now in the wx.richtext module
itself:

Traceback (most recent call last):
  File "C:\Users\project\richTextPanel.py", line 548, in OnFont
    if self.rtc.GetStyle(self.rtc.GetInsertionPoint(), attr):
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\richtext.py", line
3139, in GetStyle
    return _richtext.RichTextCtrl_GetStyle(*args, **kwargs)
TypeError: in method 'RichTextCtrl_GetStyle', expected argument 3 of type
'wxRichTextAttr &'

Any ideas?

Thanks,
Che

···

On Wed, Jan 21, 2015 at 11:36 AM, C M <cmpython@gmail.com> wrote:

I upgraded to wxPython 3.0.1.1 and it seems to have broken my use of
wx.RichTextCtrl.

However, now I am getting another error, now in the wx.richtext module
itself:

Traceback (most recent call last):
  File "C:\Users\project\richTextPanel.py", line 548, in OnFont
    if self.rtc.GetStyle(self.rtc.GetInsertionPoint(), attr):
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\richtext.py", line
3139, in GetStyle
    return _richtext.RichTextCtrl_GetStyle(*args, **kwargs)
TypeError: in method 'RichTextCtrl_GetStyle', expected argument 3 of type
'wxRichTextAttr &'

OK, maybe working. I made the attribute this:

    attr = wx.richtext.RichTextAttr()

and not wx.TextAttr() as the 2.9 Recent Changes seem to say I should use.

Then when I set the flags, used this;

    attr.SetFlags(wx.TEXT_ATTR_FONT)

and it seems to be working for the moment.

Any ideas?

Thanks,
Che

···

C M wrote:

    However, now I am getting another error, now in the wx.richtext
    module itself:

    Traceback (most recent call last):
       File "C:\Users\project\richTextPanel.py", line 548, in OnFont
         if self.rtc.GetStyle(self.rtc.GetInsertionPoint(), attr):
       File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\richtext.py",
    line 3139, in GetStyle
         return _richtext.RichTextCtrl_GetStyle(*args, **kwargs)
    TypeError: in method 'RichTextCtrl_GetStyle', expected argument 3 of
    type 'wxRichTextAttr &'

OK, maybe working. I made the attribute this:

     attr = wx.richtext.RichTextAttr()

and not wx.TextAttr() as the 2.9 Recent Changes seem to say I should use.

Then when I set the flags, used this;

     attr.SetFlags(wx.TEXT_ATTR_FONT)

and it seems to be working for the moment.

RichTextAttr is a class that derives from wx.TextAttr, and is where the RichTextCtrl-specific things are kept. Originally you could use either RichTextAttr or TextAttrEx for just about everything. Then TextAttrEx was dropped and TextAttr could be used for most methods but with some loss in functionality, and then finally RichTextAttr was changed to derive from TextAttr and the dual personality of RTC attributes was coalesced back into just one way to do things.

···

--
Robin Dunn
Software Craftsman

1 Like