Subclassing wx.Frame

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

Subclassing wx.Frame

Alex Hall
Hello list,
I was on this list years ago, using a different email address. I'm now
working for a company where, among other things, I find myself needing
to create a GUI application to let us manage and run server jobs. It's
going pretty well, but a couple parts of WX are tripping me up. I'll
start with what I hope is an easy one: subclassing wx.Frame.

In every example I can find online, you do it like this:

class MyFrame(wx.Frame):
  def __init__(self, parent, title="My Application", size=(480, 360):
    wx.Frame.__init__(parent, title, size)
    #keep doing your own initializing

I'm doing this from memory, so I might have a couple arguments wrong.
The main thing of note is the call to wx.Frame.__init__. In most
subclasses, you'd do it this way:

super(MyFrame, self).__init__(parent, title, size)

What I'm not sure of is why you use the wx.Frame.__init__ directly,
instead of using super. There's probably a good reason, but I'm coming
back to Python/WX after a couple years of playing with Swift/ObjC and
Cocoa on a Mac, so I'm just not seeing the obvious answer. I'll have
more questions, but I want to keep each in its own thread. Thanks for
any answers anyone can offer.

--
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: Subclassing wx.Frame

James Scholes
Alex Hall wrote:

> In every example I can find online, you do it like this:
>
> class MyFrame(wx.Frame):
>   def __init__(self, parent, title="My Application", size=(480, 360):
>     wx.Frame.__init__(parent, title, size)
>     #keep doing your own initializing
>
> I'm doing this from memory, so I might have a couple arguments wrong.
> The main thing of note is the call to wx.Frame.__init__. In most
> subclasses, you'd do it this way:
>
> super(MyFrame, self).__init__(parent, title, size)
>
> What I'm not sure of is why you use the wx.Frame.__init__ directly,
> instead of using super.

super was added in Python 2.2, and only works for new-style classes.  As
an awful lot of wxPython documentation is quite dated now, my guesses
would be that:

1. super wasn't a part of the language when the example(s) or
tutorial(s) were written;
2. The example(s) or tutorial(s) were written at a time that super was
available, but people were used to using old-style classes; or
3. The authors of newer tutorials and samples weren't aware of, or in
the habbit of using, super in their subclasses.  If the tutorial was
written by or for beginners and penned somewhat recently, this seems
like the most likely answer.  When I was a beginner, writing my first
wxPython code, I didn't know what super was or what it did, and the
official documentation would've probably confused me a fair bit.  So the
fact that the tutorials I was using avoided its use probably helped me
get to grips with the content with as few stumbling blocks as possible.
--
James Scholes
http://twitter.com/JamesScholes

--
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: Subclassing wx.Frame

Alex Hall
Thanks for the answer. Can I safely use super(myClass,
self).__init__(...) in my code? It just makes more sense to me to do
it that way, probably because of Swift and ObjC doing that.

On 1/28/16, James Scholes <[hidden email]> wrote:

> Alex Hall wrote:
>> In every example I can find online, you do it like this:
>>
>> class MyFrame(wx.Frame):
>>   def __init__(self, parent, title="My Application", size=(480, 360):
>>     wx.Frame.__init__(parent, title, size)
>>     #keep doing your own initializing
>>
>> I'm doing this from memory, so I might have a couple arguments wrong.
>> The main thing of note is the call to wx.Frame.__init__. In most
>> subclasses, you'd do it this way:
>>
>> super(MyFrame, self).__init__(parent, title, size)
>>
>> What I'm not sure of is why you use the wx.Frame.__init__ directly,
>> instead of using super.
>
> super was added in Python 2.2, and only works for new-style classes.  As
> an awful lot of wxPython documentation is quite dated now, my guesses
> would be that:
>
> 1. super wasn't a part of the language when the example(s) or
> tutorial(s) were written;
> 2. The example(s) or tutorial(s) were written at a time that super was
> available, but people were used to using old-style classes; or
> 3. The authors of newer tutorials and samples weren't aware of, or in
> the habbit of using, super in their subclasses.  If the tutorial was
> written by or for beginners and penned somewhat recently, this seems
> like the most likely answer.  When I was a beginner, writing my first
> wxPython code, I didn't know what super was or what it did, and the
> official documentation would've probably confused me a fair bit.  So the
> fact that the tutorials I was using avoided its use probably helped me
> get to grips with the content with as few stumbling blocks as possible.
> --
> James Scholes
> http://twitter.com/JamesScholes
>
> --
> 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.
>

--
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: Subclassing wx.Frame

James Scholes
Alex Hall wrote:
> Can I safely use super(myClass, self).__init__(...) in my code?

Absolutely.  And if you're on Python 3, you can just call:
super().__init__(...)
Instead of:
super(MyClass, self).__init__(...)
--
James Scholes
http://twitter.com/JamesScholes

--
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: Subclassing wx.Frame

Chris Barker - NOAA Federal
In reply to this post by Alex Hall
On Thu, Jan 28, 2016 at 9:26 AM, Alex Hall <[hidden email]> wrote:
Thanks for the answer. Can I safely use super(myClass,
self).__init__(...) in my code?

yes, you sure can -- super() is only really potentially different when you are doing multiple inheritance.

BTW, if you are going to be "modern" about it, use *args, **kwargs whenever you can:

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

(I still kind of like the explicitness of calling the __init__ directly...)

now you have your version of the Frame that take all the same arguments as the regular one...

-CHB



--

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

[hidden email]

--
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: Subclassing wx.Frame

Mike Driscoll
In reply to this post by Alex Hall


On Thursday, January 28, 2016 at 11:26:20 AM UTC-6, Alex Hall wrote:
Thanks for the answer. Can I safely use super(myClass,
self).__init__(...) in my code? It just makes more sense to me to do
it that way, probably because of Swift and ObjC doing that.


As Chris already mentioned, yes you can use super safely in wxPython. Here's an example:

import wx

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, *args, **kwargs):
        """Constructor"""
        super(MyFrame, self).__init__(*args, **kwargs)
        self.Show()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame(None, title='Test')
    app.MainLoop()

You might want to check out Cody Precord's book, the wxPython Cookbook from Packt. It uses super through-out as I recall.

Mike

--
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.
bht
Reply | Threaded
Open this post in threaded view
|

Re: Subclassing wx.Frame

bht
In reply to this post by Alex Hall
An even more pythonic (though more obscure) way to do this is with

   super(self.__class__,self).__init__(*args, **kwargs)

nice thing about this is that you don't need to worry if you later rename MyFrame to something else.

Brian

--
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: Subclassing wx.Frame

Alex Hall

On Jan 29, 2016, at 16:45, bht <[hidden email]> wrote:

An even more pythonic (though more obscure) way to do this is with

   super(self.__class__,self).__init__(*args, **kwargs)

I know we're straying a bit OT again, but I just have to say: that's really COOL! I always forget about the built-in __*__ attributes, and as you said, __class__ will let you not care what you name your subclass. Now I just have to figure out how *args and **kwargs work if functions that take them don't appear to unpack/iterate over them, and I'll be set. :P Off to Google I go.

nice thing about this is that you don't need to worry if you later rename MyFrame to something else.

Brian

--
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.



--
Alex Hall
Automatic Distributors, IT Department
942-6769, Ext. 629

--
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: Subclassing wx.Frame

Niki Spahiev-2
In reply to this post by bht
On 29.01.2016 23:45, bht wrote:
> An even more pythonic (though more obscure) way to do this is with
>
> super(self.__class__,self).__init__(*args, **kwargs)

Don't do that. This will stop working after second subclass. If it was
enough python authors would use it :)

Niki

--
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.