views:

163

answers:

3

I want to define several plugins. They all inherit from the superclass Plugin.

Each plugin consists on a wx.Panel that have a more specific method called "draw".

How can I define a class as a Panel and afterwards call that class in my frame?

I've tried like this:

class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel(self, parent)

but it gives me this error:

in __init__
    _windows_.Panel_swiginit(self,_windows_.new_Panel(*args, **kwargs))
TypeError: in method 'new_Panel', expected argument 1 of type 'wxWindow *'

Thanks in advance!

A: 

There is a class wx.PyPanel that is a version of Panel intended to be subclassed from Python and allows you to override C++ virtual methods.

There are PyXxxx versions of a number of other wx classes as well.

Dave Kirby
@Dave Kirby, while what you say is true, it has nothing whatsoever to do with the OP's problem, nor is it necessary unless you do have to override one of those special methods, which is fairly rare.
Peter Hansen
+3  A: 
class MyPanel(wx.Panel):
    def __init__(self, *args):
        wx.Panel.__init__(self, *args)

    def draw(self):
        # Your code here
Javier Badia
A: 

How can I define a class as a Panel and afterwards call that class in my frame?

What you tried is close, but you're not properly calling the super class __init__. When subclassing wxPython classes, however, it's generally best to use the following pattern so that you don't have to worry about which specific arguments you are passing to it. (This wouldn't have solved your problem, which was outside of the code in question, but it maybe makes it clearer what's happening.)

class Panel(wx.Panel):
    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)
        # ... code specific to your subclass goes here

This ensures that anything passed in is handed on to the super class method with no additions or removals. That means the signature for your subclass exactly matches the super class signature, which is also what someone else using your subclass would probably expect.

If, however, you are not actually doing anything in your own __init__() method other than calling the super class __init__(), you don't need to provide the method at all!

As for your original issue:

but it gives me this error: in __init__ windows.Panel_swiginit(self,windows.new_Panel(*args, **kwargs)) TypeError: in method 'new_Panel', expected argument 1 of type 'wxWindow *'

(Edited) You were actually instantiating a wx.Panel() inside the __init__ rather than calling the super class __init__, as Javier (and Bryan Oakley, correcting me) pointed out. (Javier's change of the "parent" arg to "*args" confused me... sorry to confuse you.)

Peter Hansen
@Peter: "As you discovered, that error was simply because you weren't correctly passing in reference to the parent window." -- I think that is incorrect. I think the problem is that he was calling wx.Panel() rather than wx.Panel.__init__() in the constructor.
Bryan Oakley
@Bryan, thanks for the correction! I fixed my cut-and-pasted code that had the same issue, and noted the issue.
Peter Hansen