如何根据某些搜索字符串更新 wxPython ListBox?
How can I update a wxPython ListBox based on some search string?
问题:
如何根据某些搜索字符串更新 wx.ListBox
?
实际上:
- 我有 2 个对象: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
注意:不要忘记为每个字符事件清除 ListBox
。如果您的可搜索项目列表太大,您应该使用不同于 clearing/appending 方法的方法。
编辑:
在检查了你的代码后,我按照你的意愿修复了它,没有改变太多。我使用 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
...
问题:
如何根据某些搜索字符串更新 wx.ListBox
?
实际上:
- 我有 2 个对象: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
方法 获取在 - 清除
ListBox
并将search_items
中的匹配字符串附加到ListBox
TextCtrl
中输入的字符串
注意:不要忘记为每个字符事件清除 ListBox
。如果您的可搜索项目列表太大,您应该使用不同于 clearing/appending 方法的方法。
编辑:
在检查了你的代码后,我按照你的意愿修复了它,没有改变太多。我使用 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
...