如何根据某些搜索字符串更新wxPython ListBox?

时间:2015-11-18 15:15:50

标签: python listbox wxpython

问题:

如何根据某些搜索字符串更新wx.ListBox? 实际上: - 我有2个obj:wx.TextCtrl + wx.ListBox - 操作:在wx.TextCtrl中写下文字后,应使用匹配更新列表wx.ListBox

我的代码:

def updateList(event):
    # Get all values from wx.ListBox obj 
    searchTerm = str([textareaExpectedResults.GetString(i) for i in range(textareaExpectedResults.GetCount())])
    print searchTerm
    # Get match
    matchValues =  sorted(['entry', 'test'])   
    textareaExpectedResults.Clear()
    i = 0
    for item in matchValues:
        if searchTerm.lower() in item.lower():
             i += 1
             textareaExpectedResults.Append(item)
        else:
             print "not found"
             pass

# Bind the function to search box
searchExpectedResults.Bind(wx.EVT_CHAR, updateList)

当前输出:

我开始写作时找不到

期望输出:

当我开始写作时,

获取匹配。 (如果我输入:“en”那么应用程序应该获取选项“entry”。当然,该条目存在于列表框中) 请分享一下这个提示。

编辑1:

# Basic app 

import wx
app = wx.App(redirect=False)
top = wx.Frame(None)
top.SetSize(320,280)
sizer = wx.GridBagSizer()

def on_char(event):
    getValue = searchExpectedResults.GetValue() # get the entered string in TextCtrl with GetValue method
    print getValue
    search_items = sorted(['test', 'entry']) # Create a list of all searchable items in a list
    for item in search_items:            
            if getValue in item:
                print item 
                textareaExpectedResults.Clear()
                textareaExpectedResults.Append(item) # Clear the ListBox and append the matching strings in search_items to the ListBox

searchExpectedResults = wx.TextCtrl(top, -1, "", size=(175, -1))
sizer.Add(searchExpectedResults,(2,8),(2,14),wx.EXPAND)
searchExpectedResults.Bind(wx.EVT_CHAR, on_char) # Bind an EVT_CHAR event to your TextCtrl
search_items = sorted(['test', 'entry'])
textareaExpectedResults = wx.ListBox(top, choices=search_items,  size=(270,250))
sizer.Add(textareaExpectedResults,(6,8),(2,14),wx.EXPAND)
top.Sizer = sizer
top.Sizer.Fit(top)
top.Show()
app.MainLoop()

1 个答案:

答案 0 :(得分:1)

以下是一步一步指导如何实现您的期望

  1. 创建列表中所有可搜索项目的列表,例如将其命名为search_items
  2. EVT_CHAR个活动绑定到您的TextCtrl,例如将您的事件处理程序命名为on_char
  3. 在方法on_char中,使用TextCtrl方法
  4. 获取GetValue中输入的字符串
  5. 清除ListBox并将search_items中匹配的字符串附加到ListBox
  6. 注意:不要忘记清除每个char事件的ListBox。如果您的可搜索项目'如果列表太大,则应使用与清除/追加方法不同的方法。

    编辑:

    在审核完代码后,我根据需要修改了代码,但没有太多改动。我使用了wx.EVT_KEY_UP,因为当wx.EVT_CHAR事件调用您的处理程序时,您无法获得wx.TextCtrl的最新值。如果您坚持使用wx.EVT_CHAR,则可以在def on_char(event)中使用wx.CallAfter,并提供一个保证在wx.EVT_CHAR完成后执行的回调函数。注意:您在for循环中调用了textareaExpectedResults.Clear()这是错误的,我之前也已经将其移动了for循环。

    import wx
    app = wx.App(redirect=False)
    top = wx.Frame(None)
    top.SetSize((320, 280))
    sizer = wx.GridBagSizer()
    
    def on_char(event):
        event.Skip()
        getValue = searchExpectedResults.GetValue() # get the entered string in TextCtrl with GetValue method
        print getValue
        search_items = sorted(['test', 'entry']) # Create a list of all searchable items in a list
        textareaExpectedResults.Clear()
        for item in search_items:
            if getValue in item:
                print item
                textareaExpectedResults.Append(item) # Clear the ListBox and append the matching strings in search_items to the ListBox
    
    searchExpectedResults = wx.TextCtrl(top, -1, "", size=(175, -1))
    sizer.Add(searchExpectedResults, (2, 8), (2, 14), wx.EXPAND)
    searchExpectedResults.Bind(wx.EVT_KEY_UP, on_char) # Bind an EVT_CHAR event to your TextCtrl
    search_items = sorted(['test', 'entry'])
    textareaExpectedResults = wx.ListBox(top, choices=search_items, size=(270, 250))
    sizer.Add(textareaExpectedResults, (6, 8), (2, 14), wx.EXPAND)
    top.Sizer = sizer
    top.Sizer.Fit(top)
    top.Show()
    app.MainLoop()
    

    如果您想使用wx.EVT_CHAR,请参阅此示例,了解如何使用wx.CallAfter

    ...
    def on_filter():
        getValue = searchExpectedResults.GetValue() # get the entered string in TextCtrl with GetValue method
        print getValue
        search_items = sorted(['test', 'entry']) # Create a list of all searchable items in a list
        textareaExpectedResults.Clear()
        for item in search_items:
            if getValue in item:
                print item
                textareaExpectedResults.Append(item) # Clear the ListBox and append the matching strings in search_items to the ListBox
    
    def on_char(event):
        event.Skip()
        wx.CallAfter(on_filter)
    
    ...
    searchExpectedResults.Bind(wx.EVT_CHAR, on_char) # Bind an EVT_CHAR event to your TextCtrl
    ...