Reputation: 66697
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!
Upvotes: 1
Views: 2528
Reputation: 22047
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.)
Upvotes: 0
Reputation: 4623
class MyPanel(wx.Panel):
def __init__(self, *args):
wx.Panel.__init__(self, *args)
def draw(self):
# Your code here
Upvotes: 5
Reputation: 26552
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.
Upvotes: 2