开发者

WxPython: deriving wx.ListItem but wx.ListCtrl only returns old class

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)
        sel开发者_如何学JAVAf.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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜