wxPython - 根据文本框输入动态更新列表控件

6 投票
4 回答
5096 浏览
提问于 2025-04-16 23:35

有没有人能给个例子,教我怎么做到以下这件事:

我有一个列表控件,里面显示了超过600个项目。现在我需要在这些项目中搜索用户输入的文本,并更新列表,只显示包含这个字符串的项目。

比如说,列表里有“Hello”、“Hi”和“Morning”。这时候列表会显示这三个项目。然后用户在文本框里输入“h”,列表就会缩小到“Hello”和“Hi”。如果用户输入的是“o”,那么列表就会变成“Hello”和“Morning”。

这可能吗?或者有没有其他方便的方法可以在列表控件中找到一个项目?内置的“输入时查找”功能只有在你确切知道要找什么的时候才有用,而在我的情况下,这并不适用……

谢谢,Woodpicker

4 个回答

1

这里有一个关于过滤UltimateListCtrl的例子。虽然我知道这已经是两年前的事了,但我在StackOverflow上找到的其他例子真的非常有帮助。我刚接触python和wxpython,希望这个例子对你们也有用。这个例子是从http://www.blog.pythonlibrary.org/2011/11/02/wxpython-an-intro-to-the-ultimatelistctrl/开始的。

import wx
from wx.lib.agw import ultimatelistctrl as ULC


class ULC_Panel(wx.Panel):
    """"""
    def __init__(self, parent, col_headers=None, list_data=None, options=None, dlg=None, 
            selected_list=None):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.options = options
        self.show_only_selected = False
        self.filter_string = ""

        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        okayButton = wx.Button(self, wx.ID_OK, "OK")
        okayButton.SetToolTip(wx.ToolTip("Click to close this dialog and use the selections"))
        self.Bind(wx.EVT_BUTTON, self.OnOkayCanButton, okayButton)
        hsizer.Add(okayButton, 0, wx.ALL, 5)
        canButton = wx.Button(self, wx.ID_CANCEL, "Cancel")
        canButton.SetToolTip(wx.ToolTip("Click to close this dialog and cancel selections"))
        self.Bind(wx.EVT_BUTTON, self.OnOkayCanButton, canButton)
        hsizer.Add(canButton, 0, wx.ALL, 5)
        cb_show_only = wx.CheckBox(self, -1, "Show only selected items?")
        cb_show_only.SetValue(self.show_only_selected)
        cb_show_only.SetToolTip(wx.ToolTip("Click to show only selected rows"))
        self.Bind(wx.EVT_CHECKBOX, self.EvtShowOnly, cb_show_only)
        hsizer.Add(cb_show_only, 0, wx.ALL, 5)

        self.stext = wx.StaticText(self, -1, "Filter: ", style=wx.ALIGN_LEFT)
        self.filtr = wx.TextCtrl(self, -1, "", style=wx.ALIGN_LEFT)
        self.Bind(wx.EVT_TEXT, self.OnFiltr, self.filtr)
        fsizer = wx.BoxSizer(wx.HORIZONTAL)
        fsizer.Add(self.stext, 0, wx.ALL)
        fsizer.Add(self.filtr, 1, wx.EXPAND)

        font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
        boldfont = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
        boldfont.SetWeight(wx.BOLD)
        boldfont.SetPointSize(12)

        self.ultimateList = ULC.UltimateListCtrl(self, agwStyle = wx.LC_REPORT 
                                         | wx.LC_VRULES | ULC.ULC_HAS_VARIABLE_ROW_HEIGHT
                                         | wx.LC_HRULES)


        self.checkbox = [None] * len(list_data)
        if selected_list != None:
            self.selected = selected_list
        else:
            self.selected = [False] * len(list_data)
        self.rows_max = len(list_data)
        self.rows_current = -1
        self.cols_max = len(col_headers)
        self.cols_extra = 1
        if options & ULC.ULC_MASK_CHECK:
            self.cols_extra += 1
        for i in xrange(self.cols_max+self.cols_extra):
            info = ULC.UltimateListItem()
            info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT 
            info._image = []
            info._format = 0
            info._kind = 1
            width = 150
            if i >= self.cols_extra:
                info._text = col_headers[i-self.cols_extra]
            elif i == 0:
                info._text = "Row"
                width = 35
            elif i == 1 and options & ULC.ULC_MASK_CHECK:
                info._text = "Select"
                width = 50
            self.ultimateList.InsertColumnInfo(i, info)
            self.ultimateList.SetColumnWidth(i, width)

        self.list_data = list_data
        pos = self.populate_table("")

        if pos != None:
            self.sz = self.ultimateList.GetItemRect(pos)
            self.width  = self.sz[2] + self.sz[3]
            self.height = self.sz[1]

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(hsizer, 0, wx.EXPAND)
        sizer.Add(fsizer, 0, wx.EXPAND)
        sizer.Add(self.ultimateList, 1, flag=wx.EXPAND)

        self.SetSizer(sizer)
        sizer.Fit(self)

    def EvtShowOnly(self, event):
        cb = event.GetEventObject()
        val = cb.GetValue()
        self.show_only_selected = val
        pos = self.populate_table(self.filter_string)
        print "show_only_selected val= ", val

    def EvtCheckBox(self, event):
        cb = event.GetEventObject()
        id   = event.GetId()
        val = cb.GetValue()
        self.selected[id] = val
        print "id, val= ", id, val

    def OnOkayCanButton(self, event):
        id = event.GetId()
        dlg.EndModal(id)

    def myGetNeedWH(self):
        return (self.width, self.height)

    def myGetSelectedState(self):
        return self.selected

    def populate_table(self, str_in):
        busy = wx.BusyCursor() 
        if str_in:
            str_low = str_in.lower()
        if self.options & ULC.ULC_MASK_CHECK:
            # if we have widgets in the row then we have to delete 1 row 
            # at a time (or else it leaves some of the widgets)
            i = self.rows_current
            #print "i, self.rows_max= ", i, self.rows_max
            while i >= 0:
                #print "i= ", i
                self.ultimateList.DeleteItem(i)
                i -= 1
        else:
            self.ultimateList.DeleteAllItems()
        row = -1
        for i in xrange(len(self.list_data)):
            tlwr = self.list_data[i][0].lower()
            if not str_in or tlwr.find(str_low) >= 0:
                if self.show_only_selected and self.selected[i] == False:
                    continue
                row += 1
                for j in xrange(self.cols_max+self.cols_extra):
                    if j == 0:
                        pos = self.ultimateList.InsertStringItem(row, str(row))
                    elif j == 1 and self.options & ULC.ULC_MASK_CHECK:
                        self.checkbox[i] = wx.CheckBox(self.ultimateList, id= i)
                        self.checkbox[i].SetValue(self.selected[i])
                        self.checkbox[i].SetToolTip(wx.ToolTip("Click to select this row"))
                        self.Bind(wx.EVT_CHECKBOX, self.EvtCheckBox, self.checkbox[i])
                        self.ultimateList.SetItemWindow(pos, col=1, wnd=self.checkbox[i], expand=False)
                    else:
                        self.ultimateList.SetStringItem(row, j, self.list_data[i][j-self.cols_extra])
        self.rows_current = row
        return row

    def OnFiltr(self, event):
        str1 = event.GetString()
        id   = event.GetId()
        #print "got txt_tval str[%s]= %s" % (id, str1)
        self.filter_string = str1
        pos = self.populate_table(str1)
        event.Skip()
        return


########################################################################
class FilterListDiag(wx.Dialog):
    def __init__(self, parent, id, title, headers=None, data_table=None, options=None, selected_list=None):
        wx.Dialog.__init__(self, parent, id, title="", style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
        options_in = options

        self.panel = ULC_Panel(self, col_headers=headers, list_data=data_table, options=options_in, 
                dlg=self, selected_list=selected_list)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel, 1, wx.EXPAND)
        self.SetSizer(sizer)
        (self.width, self.height) = self.panel.myGetNeedWH()

    def myGetNeedWH(self):
        return (self.width, self.height)

    def myGetSelectedState(self):
        return self.panel.myGetSelectedState()

class TestFrame(wx.Frame):
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="MvP UltimateListCtrl Demo",  size=(850,600))

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = TestFrame()
    col_headers = ['col0', 'col1', 'col2']
    list_data = [
            ["Newsboys", "Go", "Rock"],
            ["Puffy", "Bring It!", "Pop"],
            ["Family Force 5", "III", "Pop"],
            ["Me2", "III", "Pop"],
            ["Duffy", "III", "Pop"],
            ["Fluffy", "III", "Pop"],
    ]
    # sel_data passes in a list of which rows are already selected
    sel_data = [  
            False,
            False,
            False,
            False,
            True,
            False,
    ]
    opt=ULC.ULC_MASK_CHECK # just reusing this to indicate I want a column of checkboxes.
    dlg = FilterListDiag(frame, -1, "hi", headers=col_headers, data_table=list_data, options=opt, selected_list=sel_data)
    (w, h) = dlg.myGetNeedWH()
    print w,h
    dlg.SetSizeWH(w, 300)
    val = dlg.ShowModal()
    selected = dlg.myGetSelectedState()
    print "okay, can, val= ", wx.ID_OK, wx.ID_CANCEL, val
    dlg.Destroy()
    print 'selected=', selected
3

我觉得ObjectListView这个封装比直接使用wx.ListCtrl要好。它有一个很棒的功能,就是可以在控件中筛选项目。你可以在这里了解更多信息:http://objectlistview.sourceforge.net/python/features.html#filtering,这是这个控件的主页:http://objectlistview.sourceforge.net/python/

4

wxPython的演示程序里有一个相当不错的“自动补全”过滤器。查看一下Main.py的源代码,他们是用“手动”的方式来实现的,也就是通过循环来重新构建列表。他们使用的是树形视图,但这些思路都是很靠谱的:

def OnSearch(self, event=None):

    value = self.filter.GetValue()
    if not value:
        self.RecreateTree()
        return

    wx.BeginBusyCursor()

    for category, items in _treeList:
        self.searchItems[category] = []
        for childItem in items:
            if SearchDemo(childItem, value):
                self.searchItems[category].append(childItem)

    wx.EndBusyCursor()
    self.RecreateTree()    

撰写回答