是否可以将 wx.Panel 定义为 Python 中的类?

发布于 2024-08-21 12:21:59 字数 463 浏览 7 评论 0原文

我想定义几个插件。 它们都继承自超类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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋心╮凉 2024-08-28 12:21:59
class MyPanel(wx.Panel):
    def __init__(self, *args):
        wx.Panel.__init__(self, *args)

    def draw(self):
        # Your code here
class MyPanel(wx.Panel):
    def __init__(self, *args):
        wx.Panel.__init__(self, *args)

    def draw(self):
        # Your code here
风透绣罗衣 2024-08-28 12:21:59

有一个类 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.

只是在用心讲痛 2024-08-28 12:21:59

如何将一个类定义为面板,然后在我的框架中调用该类?

您尝试的方法很接近,但您没有正确调用超类 __init__。然而,当子类化 wxPython 类时,通常最好使用以下模式,这样您就不必担心要传递给它的特定参数。 (这不会解决您的问题,该问题超出了相关代码的范围,但它可能使发生的事情变得更清楚。)

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

这确保了传入的任何内容都会传递给超类方法,而无需添加或删除。这意味着您的子类的签名与超类签名完全匹配,这也是使用您的子类的其他人可能期望的。

但是,如果除了调用超类 __init__() 之外,您实际上没有在自己的 __init__() 方法中执行任何操作,则无需提供该方法根本!

至于你原来的问题:

但它给了我这个错误:在 __init__ windows.Panel_swiginit(self,windows.new_Panel(*args, **kwargs)) 中 TypeError: 在方法 'new_Panel' 中,预期参数 1 类型为 'wxWindow *'

已编辑)您实际上是在 __init__ 中实例化 wx.Panel(),而不是调用超类 __init__,如 Javier(和布莱恩·奥克利(Bryan Oakley)纠正了我)指出。 (哈维尔将“parent”arg 更改为“*args”让我感到困惑......很抱歉让您感到困惑。)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文