Init Question

Hi,

  See this code?:

···

--------------------------------
class MyTreeCtrl(wx.TreeCtrl):
    def __init__(self, parent, id, pos, size, style, log):
        wx.TreeCtrl.__init__(self, parent, id, pos, size, style)
--------------------------------

  I still can't understand why you instantiate the wx.TreeCtrl (or any
other object, for that matter), def __init__ the TreeCtrl, and then
__init__ it again. What's with initiating it twice? It was already
done, right? Thank you.

Telly

This the way subclassing works in Python. Unless you want your
TreeCtrl to do something extra, you don't need to subclass it, just
make an instance of it with

MyTree = wx.TreeCtrl(parent,id,pos,size,style)

···

On 2/22/07, Telly Williams <TWilliams001@elp.rr.com> wrote:

Hi,

        See this code?:

--------------------------------
class MyTreeCtrl(wx.TreeCtrl):
    def __init__(self, parent, id, pos, size, style, log):
        wx.TreeCtrl.__init__(self, parent, id, pos, size, style)
--------------------------------

        I still can't understand why you instantiate the wx.TreeCtrl (or any
other object, for that matter), def __init__ the TreeCtrl, and then
__init__ it again. What's with initiating it twice? It was already
done, right? Thank you.

Telly

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

--
Josh English
Joshua.R.English@gmail.com

Telly Williams wrote:

--------------------------------
class MyTreeCtrl(wx.TreeCtrl):
    def __init__(self, parent, id, pos, size, style, log):
        wx.TreeCtrl.__init__(self, parent, id, pos, size, style)
--------------------------------

  I still can't understand why you instantiate the wx.TreeCtrl (or any
other object, for that matter), def __init__ the TreeCtrl, and then
__init__ it again. What's with initiating it twice? It was already
done, right? Thank you.

This is very standard subclassing in Python. You need to manually call the superclass' __init__ if you want it to run. Otherwise, your __init__ has over-ridden the superclass' one, so it will never get called.

If there is nothing to be done in the subclass __init__, you can not define one, and then the superclass' one will get called.

You really need to understand OO in Python to be able to use wxPython:

Note that the first entry in:

http://wiki.wxpython.org/index.cgi/How_to_Learn_wxPython

is: "Learn Python"

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Thanks, Josh,

  What something extra would you want it do that you can't already do
with a normal instance?

Telly

Telly Williams wrote:

Thanks, Josh,

  What something extra would you want it do that you
can't already do with a normal instance?

Telly

Hi Telly,

First, it is good practice to leave enough of the previous message in your
reply so that someone reading only your reply can figure out what it is
about without having to go back and dig out all the previous messages in the
thread. On the other hand, don't go to the other extreme of keeping the
entire original message in your reply, otherwise it justs gets longer and
longer if there are a number of replies. Delete what is not relevant, keep
what is relevant to the point that you want to make.

As has been suggested, you really do need to read up on how Object
Orientation and Inheritance work in Python. However, I will try to explain.

You can create an instance of a wx.TreeCtrl like this -

    tree = wx.TreeCtrl(parent,id,pos,size,style)

When this code is executed, Python checks to see if wx.TreeCtrl has a method
called __init__, and if so it calls it. You can be sure that wx.TreeCtrl
does have an __init__ method, and that it does a whole lot of important
stuff that you really do not want to know about :wink:

Alternatively you can create a subclass of wx.TreeCtrl, like this -

    class MyTreeCtrl(wx.TreeCtrl):
        def __init__(self, parent, id, pos, size, style, log):
            [do your own stuff here]

    tree = MyTreeCtrl(parent,id,pos,size,style,log)

When this code is executed, Python checks to see if MyTreeCtrl has a method
called __init__, and if so it calls it. If it does not find one, it checks
the parent class, wx.TreeCtrl, to see if it has one, and if so, it calls it.

However, and this is the important point, if MyTreeCtrl has an __init__
method, as it does in your example, Python will call the method, but it will
*not* call the corresponding method in the parent class. This is how
inheritance works - you have 'overridden' the parent class's method with
your own. If you want the __init__ method in the parent class to be called,
it is up to you to call it explicitly from within your __init__ method. If
you do not do this, none of the important stuff in wx.TreeCtrl will happen,
and you can be sure it will not work properly.

Hope this helps

Frank Millman

In my current project I use panels that are one row of checkboxes. I
don't like using wxPythons built in checklistbox because it's only
vertical (if it can be horizontal, I'd be happy). So I subclass
wx.Panel :

lass CheckPanel(wx.Panel):
    def __init__(self,*args,**kwds):
        self.choices = kwds.pop('choices')
        wx.Panel.__init__(self,*args,**kwds)
        #self.choices = choices
        self.checks =
        self.cdict = {}
        box = wx.BoxSizer(wx.HORIZONTAL)
        for ch in self.choices:
            cb = wx.CheckBox(self,-1,ch)
            self.checks.append((ch,cb))
            self.cdict[ch]=cb
            box.Add(cb,0)
        self.SetSizer(box)

    def GetChecks(self):
        """Returns a comma-delimited string of checked boxes"""
        res =
        for ch,st in self.checks:
            if st.IsChecked(): res.append(ch)
        # Why did I use '-s%s'? I don't think I should,
        # since some are used to enter information, not just
        # generate a filter option
        s = ', '.join(['%s' % r for r in res])
        return s

    def ClearAll(self):
        for ch,st in self.checks:
            st.SetValue(False)

    def CheckAll(self):
        for rh,st in self.checks:
            st.SetValue(True)

    def SetChecks(self,s=""):
        """Takes a comma-delimited string and checks the appropriate boxes"""
        self.ClearAll()
        s = s.split(',')
        s = map(strip,s)
        for i in s:
            if i in self.choices:
                self.cdict[i].SetValue(True)
            else:
                msg = 'Something has gone wrong!\nCannot check the %s
box.\n\nThis is somethnig the programmer should know about' % i
                dlg = wx.MessageDialog(self,msg,'Oops',wx.OK|wx.ICON_ERROR)
                dlg.ShowModal()
                dlg.Destroy()

Normal panels won't do this. I mostly need these checkboxes to be set
and read. I need to add to this, because I want each box, when
checked, to call a method. For this project it is only one method, no
matter which box in checked. Checking a box on the panel needs to tell
the program that a box has beet checked, it doesn't matter which one.

Now that I've subclassed the panel, I call subclass again for my application:

class GenreCheckBox(CheckPanel):
    def __init__(self,*args,**kwds):
        kwds['choices'] = cp.GetGenres()
        CheckPanel.__init__(self,*args,**kwds)

This may not be the easiest solution, but it works for me. I need
instances of the GenreCheckBox in several places throughout my
application.

So I hope any of this helps.

Josh

···

On 2/22/07, Telly Williams <TWilliams001@elp.rr.com> wrote:

Thanks, Josh,

        What something extra would you want it do that you can't already do
with a normal instance?

Telly

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

--
Josh English
Joshua.R.English@gmail.com