WxPython:派生wx.ListItem但wx.ListCtrl仅返回旧类
我在派生类方面遇到了一个小问题,即 wx.ListItem
和 wx.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.filelist
是 wx.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我应该忍住,然后回归到次优的手动记账。当做得有品味时,这没什么大不了的,但我对 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 awx.ListItem
back to wxPython.My "solution" thus is to derive
wx.ListCtrl
instead ofwx.ListItem
, store needed information and control the appearances there.