thelost
thelost

Reputation: 705

Set String of the Current Selected Item with wxListbox

I have a listbox,

How can I change the string of current selected item of the listbox to another string?

I cant really find how to do this on Google.

Upvotes: 0

Views: 1037

Answers (1)

Andrey Sobolev
Andrey Sobolev

Reputation: 12693

Just delete selected string and insert a new one, like it is done in this example for a single-choice listbox:

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()

If you need multiple-selection listbox, then you should create it with style=wx.LB_MULTIPLE:

        self.lb = wx.ListBox(self, -1, choices = ('One', 'Two'), style=wx.LB_MULTIPLE)

Now you're able to change multiple strings at once:

    def ButtonPress(self, evt):
        txt = self.tc.GetValue()
        for pos in self.lb.GetSelections():
            self.lb.Delete(pos)
            self.lb.Insert(txt, pos)

Upvotes: 1

Related Questions