WxPython:派生wx.ListItem但wx.ListCtrl仅返回旧类

发布于 2024-09-07 12:36:38 字数 858 浏览 4 评论 0原文

我在派生类方面遇到了一个小问题,即 wx.ListItemwx.ListCtrl。我成功地将 wx.ListItem 派生为 MediaItem,代码尚未完成,但您明白了:

class MediaItem(wx.ListItem):
    def __init__ (self, fullname):
        wx.ListItem.__init__(self)
        self.fullname = fullname
        self.filename = os.path.basename(fullname)
        # snap...

    def getFullname(self):
        return self.fullname

wx.ListCtrl 很高兴接受这一点,因为蟒蛇鸭哲学。但现在的问题是,使用wx.ListCtrl.GetItem(index)方法返回一个ListItem,而不是MediaItem。 Python 抱怨 wx.ListItem 没有属性 getFullname

转换对象似乎是解决问题的错误方法。这可能与问题无关,但我也粘贴了有问题的行:

filename = self.filelist.GetItem(event.GetIndex()).getFullname()

其中 self.filelistwx.ListCtrl

I've got a small issue with derived classes, namely wx.ListItem with wx.ListCtrl. I succesfully derived wx.ListItem as a MediaItem, the code is not finished but you get the point:

class MediaItem(wx.ListItem):
    def __init__ (self, fullname):
        wx.ListItem.__init__(self)
        self.fullname = fullname
        self.filename = os.path.basename(fullname)
        # snap...

    def getFullname(self):
        return self.fullname

wx.ListCtrl gladly accepts that because of Pythons duck philosophy. But now the problem is that using the method wx.ListCtrl.GetItem(index) returns a ListItem, not MediaItem. Python complained about wx.ListItem not having an attribute getFullname.

Casting objects seems to be the wrong way to approach the solution. This probably has nothing to do with the problem, but I paste the offending line as is as well:

filename = self.filelist.GetItem(event.GetIndex()).getFullname()

Where self.filelist is a wx.ListCtrl.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

高冷爸爸 2024-09-14 12:36:38

我想我应该忍住,然后回归到次优的手动记账。当做得有品味时,这没什么大不了的,但我对 wxPython 抱有更高的希望。

据推测(根据我搜索和收集的内容)问题在于 wxPython 类基的代理性质。如果它们是用纯 Python 编写的,或者我用 C++ 编写的,效果会很好。但现在由于设计的限制,对象的多态性失败了:原生 C++ wx 类除了 wx.ListItem 之外什么也得不到,而且它肯定只会返回一个 wx.ListItem< /code> 回到 wxPython。

因此,我的“解决方案”是派生 wx.ListCtrl 而不是 wx.ListItem,存储所需的信息并控制那里的外观。

I guess I should just suck it up, and regress to the suboptimal manual bookkeeping. When done tastefully, it's not a big deal but I had higher hopes for wxPython.

Supposedly (from what I searched and collected) the issue is with the proxy nature of wxPython class base. Were they written in pure Python, or I coded in C++, this would have worked well. But now the polymorphism of objects fails because of limitations in the design: the native C++ wx class won't get anything but a wx.ListItem and it will certainly only return a wx.ListItem back to wxPython.

My "solution" thus is to derive wx.ListCtrl instead of wx.ListItem, store needed information and control the appearances there.

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