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

4

问题:

如何根据搜索字符串更新wx.ListBox? 实际上: - 我有两个对象: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()

你可以使用 wx.SearchCtrl 来让你的表单看起来更漂亮。它和 wx.TextCtrl 的用法一样,只是增加了起始字符串和取消按钮。 - hdrz
1个回答

1
这是一个逐步指南,告诉你如何实现你的期望:
  1. 创建一个包含所有可搜索项目的列表,例如将其命名为search_items
  2. EVT_CHAR事件绑定到你的TextCtrl上,例如将事件处理程序命名为on_char
  3. on_char方法中,使用GetValue方法获取TextCtrl中输入的字符串
  4. 清除ListBox并将匹配的字符串附加到search_itemsListBox

注意:不要忘记在每个字符事件中清除ListBox。如果可搜索项列表太大,则应使用不同的方法而非清除/附加方法。


编辑:

在审查了您的代码后,我按照您的要求进行了修复,而没有做太多更改。我使用了wx.EVT_KEY_UP,因为当您的处理程序被wx.EVT_CHAR事件调用时,您无法获取wx.TextCtrl的最新值。如果您坚持使用wx.EVT_CHAR,您可以在def on_char(event)中使用wx.CallAfter,通过提供一个回调函数来保证在wx.EVT_CHAR完成后执行。注意:您在循环中调用了textareaExpectedResults.Clear(),这是错误的,我也将其移到了循环之前。

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
...

我已经尝试了你的方法,但是还不能完全让它工作。def on_char(event): getValue = searchExpectedResults.GetValue() # 使用GetValue方法获取TextCtrl中输入的字符串 print getValue search_items = sorted(['test', 'entry']) # 创建一个包含所有可搜索项的列表 for item in search_items:
if getValue in item: print item textareaExpectedResults.Clear() textareaExpectedResults.Append(item) # 清空ListBox并将匹配的字符串附加到其中
- george
你能否编辑你的问题并附上你已经完成的内容? - Ozan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接