I want to be able to select multiple ListCtrl items and process them all at once. What is happening is that the wx.EVT_LIST_ITEM_SELECTED event is firing for each selected item so when I run the following code and select a range of items
import wx
class MyApp(wx.App):
def OnInit(self):
frame = Frame()
return True
class Frame(wx.Frame):
def __init__(self,parent=None,id=-1):
wx.Frame.__init__(self, None, -1, 'Show Groups')
self.Bind(wx.EVT_CLOSE,self.OnExit,self)
screenw, screenh = wx.DisplaySize()
self.panel = Panel(self)
self.SetSize(self.panel.list.GetBestSize()[0], screenh - 30)
self.Move(screenw-self.Size.width,0)
self.Fit()
self.Show()
def OnExit(self,event):
self.Destroy()
class Panel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
sizer = wx.BoxSizer(wx.VERTICAL)
text = "Time exists in so that everything doesn't happen all at once and space exists so that it doesn't all happen to you"
self.list = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
self.list.InsertColumn(0, 'Name')
self.list.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelected)
self.index = 0
for word in text.split():
self.add_line(word)
sizer.Add(self.list, 1, wx.ALIGN_LEFT | wx.EXPAND, 0)
self.SetSizer(sizer)
def OnSelected(self, evt):
print("\nOnSelected", self.list.GetSelectedItemCount())
item = self.list.GetFirstSelected()
while item != -1:
data = self.list.GetItem(item)
print(data.Text)
item = self.list.GetNextSelected(item)
def add_line(self, line):
self.list.InsertItem(self.index, line)
self.index += 1
app = wx.App(False)
frame = Frame()
app.MainLoop()
I get the following output
>>>
============================ RESTART: D:\test.py ============================
OnSelected 1
happen
OnSelected 2
happen
all
OnSelected 3
happen
all
at
OnSelected 4
happen
all
at
once
OnSelected 5
happen
all
at
once
and
OnSelected 6
happen
all
at
once
and
space
OnSelected 7
happen
all
at
once
and
space
exists
Is there an event I can use to only process the selected items when I have the entire list?