2011-08-14 27 views
5

不會有人對你有一個例子,如何做出如下可能:wxPython的 - dynamially更新根據輸入的listctrl成textctrl

我有一個顯示> 600項的listctrl。現在我需要在這些項目中搜索用戶輸入的文本並更新列表以僅顯示包含此字符串的項目。

那麼讓我們說列表包含「你好」,「嗨」和「早上」。該列表顯示全部三個項目。現在用戶在文本控件中鍵入「h」,並將listctrl縮小爲「Hello」和「Hi」。如果用戶改爲輸入「o」,並且列表變爲「Hello」和「Morning」。

這可能嗎?或者有沒有其他方便的方法來查找listctrl中的項目?在「發現當你輸入」構建只是真正的使用,如果你確切地知道你的搜索內容 - 在我的情況下,這不會真的是這樣......

感謝,Woodpicker

回答

4

的wxPython演示內置了一個相當不錯的「提前輸入」過濾器。看看source code to Main.py他們這樣做的「手動方式」,循環和重建列表。他們使用一個TreeView但這些想法是合理的:

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()  
+0

「Main.py」的超鏈接已損壞。這裏有一個工作鏈接(2017-12-07)http://svn.wxwidgets.org/svn/wx/wxPython/trunk/demo/Main.py?view=markup由於wxPython已經轉移到github,這裏是最新的鏈接如果svn回購被刪除:https://github.com/wxWidgets/wxPython/blob/master/demo/Main.py – IceArdor

3

我喜歡ObjectListView包裝器比直wx.ListCtrl更好。它包括將控件中的項目過濾爲小部件的功能。你可以在這裏閱讀:http://objectlistview.sourceforge.net/python/features.html#filtering這裏是控制的主頁:http://objectlistview.sourceforge.net/python/

+0

好東西知道。可悲的是,在這個特定的項目中,我只貢獻,因此被迫與現有的listctrl ... – Woodpicker

+1

好吧,因爲ObjectListView基本上是ListCtrl的包裝,你可以看看源代碼。那麼也許你可以複製他的方法。 –

1

這是一個過濾UltimateListCtrl的例子。 我知道這是2年後,但我發現其他例子在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