Python - ObjectListView过滤器

时间:2015-03-10 17:07:28

标签: python filter wxpython objectlistview objectlistview-python

我已阅读here,并尝试使用Filter.Predicate(booleanCallable)过滤ObjectListView,如下所示:

class Book(object):
    def __init__(self,name,author,length):
        self.name=name
        self.author=author
        self.length=str(length)


class ObjectDataView(ObjectListView):
    def __init__(self, parent):
        ObjectListView.__init__(self, parent, size=(400,400), pos=(0,0), style=wx.LC_REPORT | wx.SUNKEN_BORDER)

        self.SetColumns([
            ColumnDefn("Name", "left", 400/3, "name"),
            ColumnDefn("Author", "left", 400/3, "author"),
            ColumnDefn("Length", "left", 400/3, "length"),

        ])

    def filter_view(self):
        self.SetFilter(Filter.Predicate(self.filter_me))
        self.Update()

    def filter_me(self,obj):
        return obj.length == '10'


app=wx.App()
frame=wx.Frame(None,-1,"Check",size=(400,400))
panel=wx.Panel(frame,size=frame.GetSize())

view = ObjectDataView(panel)
view.AddObject(Book("hary1","ori",10))
view.AddObject(Book("hary2","ori",10))
view.AddObject(Book("hary3","s",15))
view.AddObject(Book("hary4","s",15))
view.AddObject(Book("hary5","s",15))

view.filter_view()
frame.Show()
app.SetTopWindow(frame)
app.MainLoop()

但是当我打电话给filter_view()时它没有做任何事情。我使用ObjectListView,因为它的一个功能是buitin Filter。我也尝试使用self.SetFilter(self.filter_me),但它也没有用。

1 个答案:

答案 0 :(得分:1)

将您的filter_view更改为:

def filter_view(self):
    self.SetFilter(olv.Filter.Predicate(self.filter_me))
    self.RepopulateList()

参见:ObjectListView.SetFilter