是否可以将 wx.Panel 定义为 Python 中的类?
我想定义几个插件。 它们都继承自超类Plugin。
每个插件都包含一个 wx.Panel,它有一个更具体的方法,称为“draw”。
如何将一个类定义为面板,然后在我的框架中调用该类?
我已经尝试过这样的:
class Panel(wx.Panel):
def __init__(self, parent):
wx.Panel(self, parent)
但它给了我这个错误:
in __init__
_windows_.Panel_swiginit(self,_windows_.new_Panel(*args, **kwargs))
TypeError: in method 'new_Panel', expected argument 1 of type 'wxWindow *'
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个类
wx.PyPanel
,它是一个旨在从 Python 派生子类的 Panel 版本,并允许您重写 C++ 虚拟方法。还有许多其他 wx 类的 PyXxxx 版本。
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.
您尝试的方法很接近,但您没有正确调用超类
__init__
。然而,当子类化 wxPython 类时,通常最好使用以下模式,这样您就不必担心要传递给它的特定参数。 (这不会解决您的问题,该问题超出了相关代码的范围,但它可能使发生的事情变得更清楚。)这确保了传入的任何内容都会传递给超类方法,而无需添加或删除。这意味着您的子类的签名与超类签名完全匹配,这也是使用您的子类的其他人可能期望的。
但是,如果除了调用超类
__init__()
之外,您实际上没有在自己的__init__()
方法中执行任何操作,则无需提供该方法根本!至于你原来的问题:
(已编辑)您实际上是在
__init__
中实例化 wx.Panel(),而不是调用超类__init__
,如 Javier(和布莱恩·奥克利(Bryan Oakley)纠正了我)指出。 (哈维尔将“parent”arg 更改为“*args”让我感到困惑......很抱歉让您感到困惑。)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.)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:
(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.)