问题:
如何根据搜索字符串更新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