我有一个列表框,
如何将列表框中当前所选项目的字符串更改为另一个字符串?
我无法在谷歌上找到如何做到这一点。
答案 0 :(得分:1)
只需删除选定的字符串并插入一个新字符串,就像在此示例中为单选列表框所做的那样:
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, style=wx.DEFAULT_FRAME_STYLE)
self.button = wx.Button(self, -1, "Change")
self.Bind(wx.EVT_BUTTON, self.ButtonPress, self.button)
self.tc = wx.TextCtrl(self, -1)
self.lb = wx.ListBox(self, -1, choices = ('One', 'Two'))
box = wx.BoxSizer(wx.VERTICAL)
box.Add(self.lb, 0, wx.EXPAND, 0)
box.Add(self.tc, 0, wx.EXPAND, 0)
box.Add(self.button, 0, wx.ADJUST_MINSIZE, 0)
self.SetSizer(box)
box.Fit(self)
self.Layout()
def ButtonPress(self, evt):
txt = self.tc.GetValue()
pos = self.lb.GetSelection()
self.lb.Delete(pos)
self.lb.Insert(txt, pos)
if __name__ == "__main__":
app = wx.PySimpleApp(0)
frame = MyFrame(None, -1, "")
frame.Show()
app.MainLoop()
如果您需要多选列表框,则应使用style=wx.LB_MULTIPLE
:
self.lb = wx.ListBox(self, -1, choices = ('One', 'Two'), style=wx.LB_MULTIPLE)
现在您可以一次更改多个字符串:
def ButtonPress(self, evt):
txt = self.tc.GetValue()
for pos in self.lb.GetSelections():
self.lb.Delete(pos)
self.lb.Insert(txt, pos)