问题:
如何根据某些搜索字符串更新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()
以下是如何实现预期的分步指南
- 创建列表中所有可搜索项目的列表,例如将其命名为
search_items
- 将
EVT_CHAR
事件绑定到TextCtrl
,例如将事件处理程序命名为on_char
- 在方法
on_char
中,使用GetValue
方法获取TextCtrl
中输入的字符串 - 清除
ListBox
,并将search_items
中的匹配字符串附加到ListBox
注意:不要忘记为每个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
...