views:

36

answers:

1

I'm trying to create a custom control based on wx.richtext.RichTextCtrl and I'm running into a problem. Whenever I attempt to add the custom control to a sizer, wxPython chokes with the error

Traceback (most recent call last):
  File "pyebook.py", line 46, in <module>
    frame = MainFrame(None, 'pyebook')
  File "pyebook.py", line 14, in __init__
    self.mainPanel.GetSizer().Add(ReaderControl(self.mainPanel), 1, wx.ALIGN_CENTER_VERTICAL | wx.EXPAND)
  File "/usr/local/lib/wxPython-unicode-2.8.11.0/lib/python2.6/site-packages/wx-2.8-mac-unicode/wx/_core.py", line 12685, in Add
    return _core_.Sizer_Add(*args, **kwargs)
TypeError: wx.Window, wx.Sizer, wx.Size, or (w,h) expected for item

The custom control is at this time extremely simple and looks like this

class ReaderControl(wx.richtext.RichTextCtrl):
    def __init__(self, parent, id=-1, value=''):
        wx.richtext.RichTextCtrl(parent, id, value, style=wx.richtext.RE_READONLY, name='ReaderControl')

The code I'm using to add the control to the sizer is:

self.mainPanel.GetSizer().Add(ReaderControl(self.mainPanel), 1, wx.ALIGN_CENTER_VERTICAL | wx.EXPAND)

Any ideas what I'm doing wrong here?

+1  A: 

I think you need to call __ init __ explicitly, so you can pass in 'self'. Otherwise, you are just creating a new instance of RichTextCtrl, not initialising your subclass properly.

IOW:

class ReaderControl(wx.richtext.RichTextCtrl):
    def __init__(self, parent, id=-1, value=''):
        wx.richtext.RichTextCtrl.__init__(self, parent, id, value, style=wx.richtext.RE_READONLY, name='ReaderControl'
JimG
Bah! I thought I _had_ called `__init__` explicitly. It's surprising how often your eyes skip errors that you should know better than to make.
Chinmay Kanchi
Yeah, sometimes you're just so busy looking for something complicated, a simple thing will slide right by. Happens to me at least once a month ;)
JimG