Confusing inheritence

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Confusing inheritence

Ed Leafe-2
Playing around more with simple examples trying to uncover an issue withSetStatusBar, and I ran into something else. Consider this script:

import wx

class SimpleMixin():
    def __init__(self, parent, title=None):
        print("Mixin init")
        self._givenTitle = title or "Default"
        super(SimpleMixin, self).__init__()
        wx.CallAfter(self.setStatusBar)
        print("Mixin init DONE")

    def setStatusBar(self):
        print("setStatusBar called")
        sb = wx.StatusBar(self)
        self.SetStatusBar(sb)
        print("StatusBar set")


#class SimpleFrame(wx.Frame, SimpleMixin):
class SimpleFrame(SimpleMixin, wx.Frame):
    def __init__(self, parent, title=None):
        print("Subclass init")
        super(SimpleFrame, self).__init__(parent, title=title)
        print("Subclass init DONE")

if __name__ == "__main__":
    app = wx.App()
    f = SimpleFrame(None, "Test")
    f.Show()
    app.MainLoop()

So I ran it with the current class definition for SimpleFrame, and I get the following printed:

Subclass init
Mixin init
Mixin init DONE
Subclass init DONE

The form never appears, nor does the CallAfter to setStatusBar get run. But it does appear that super() is working correctly. So I switched the order of the classes so that wx.Frame comes first, and now I get:

Subclass init
Subclass init DONE

Now the form appears, but the mixin class never gets initialized, and, of course, the status bar never gets set.

What am I missing?

-- Ed Leafe


--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Confusing inheritence

Robin Dunn


On Monday, January 28, 2019 at 12:11:32 PM UTC-8, Ed Leafe wrote:
[...]
 
So I ran it with the current class definition for SimpleFrame, and I get the following printed:

Subclass init
Mixin init
Mixin init DONE
Subclass init DONE

The form never appears, nor does the CallAfter to setStatusBar get run. But it does appear that super() is working correctly. So I switched the order of the classes so that wx.Frame comes first, and now I get:

Subclass init
Subclass init DONE

Now the form appears, but the mixin class never gets initialized, and, of course, the status bar never gets set.

What am I missing?


When you invoke the super's __init__ you call it like this:

        super(SimpleMixin, self).__init__()

But that will call wx.Frame's default constructor (IOW, wx.Frame(), the same as the old wx.PreFrame()) so the UI portion of the window is never called, so MainLoop exits immediately, and so on... If you call it like this instead:

        super(SimpleMixin, self).__init__(parent, title=title)

Then wx.Frame's __init__ will be invoked with those args and things will work like you are expecting.
 
--
Robin

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Confusing inheritence

Ed Leafe-2
On Monday, January 28, 2019 at 3:51:07 PM UTC-6, Robin Dunn wrote:

When you invoke the super's __init__ you call it like this:

        super(SimpleMixin, self).__init__()

But that will call wx.Frame's default constructor (IOW, wx.Frame(), the same as the old wx.PreFrame()) so the UI portion of the window is never called, so MainLoop exits immediately, and so on... If you call it like this instead:

        super(SimpleMixin, self).__init__(parent, title=title)

Then wx.Frame's __init__ will be invoked with those args and things will work like you are expecting.


Ugh! Bitten again by that overloading! Thanks, Robin!

-- Ed 

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.