我如何更新wx.ListBox
基於某些搜索字符串? 實際上: - 我有2個OBJ文件: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)
:一旦文本在
wx.TextCtrl
寫下來,列表
wx.ListBox
應該用火柴
我的代碼更新
當前輸出:
當我開始寫作時未找到。
所需的輸出:
取火柴,當我開始書面方式。 (如果我輸入:「en」,那麼應用程序應該獲取「條目」選項。當然,該條目存在於列表框中) 請分享此提示。
編輯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