Hi,
I'm new to wxpython and I keep getting an "invalid item index in
GetItemState" error after deleting items in my listctrl.
full script below.
any help appreciated.
Thanks!
···
==
import wx
import sys
import UltimateListCtrl as ULC
from collections import defaultdict
packages = [('alba', 'pomona', '1981'), ('weaver', 'new york',
'1949'),
('jolie', 'los angeles', '1975'), ('portman', 'jerusalem',
'1981'),
('john', 'london', '1971'), ('johansson', 'new york', '1984' ),
('sandy', 'paris', '1971'), ('garcia', 'new york', '1984' ),
('zeus', 'atlanta', '1971'), ('jones', 'new york', '1984' ),
('honda', 'calcutta', '1971'), ('smith', 'new york', '1984' )]
packages= [(str(i),)+packages[i] for i in range(len(packages))]
class Actresses(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, -1)
hbox = wx.BoxSizer(wx.HORIZONTAL)
self.list = ULC.UltimateListCtrl(self, -1, style=wx.LC_REPORT)
self.list.InsertColumn(0, 'index', width=50)
self.list.InsertColumn(1, 'name', width=140)
self.list.InsertColumn(2, 'place', width=130)
self.list.InsertColumn(3, 'year', wx.LIST_FORMAT_RIGHT, -1)
for i in packages:
index = self.list.InsertStringItem(sys.maxint, i[0])
self.list.SetStringItem(index, 1, i[1])
self.list.SetStringItem(index, 2, i[2])
self.list.SetStringItem(index, 3, i[3])
self.list.SetItemData(index,i)
self.chk_item = dict() # maps IDs to items
self.chk_button = dict() # maps IDs to buttons
self.chk_index = dict() #maps IDs to row indices
#self.list_dict = defaultdict(dict)
for j in range(1,4): # across columns
for i in range(len(packages)): # down rows
item = self.list.GetItem(i,j)
chk = wx.CheckBox(self.list,id=-1)
item.SetWindow(chk)
self.chk_item[chk.GetId()]= item
self.chk_button[chk.GetId()] = chk
self.chk_index[chk.GetId()] = i
#self.list_dict[chk.GetId()]['index']
self.list.SetItem(item)
hbox.Add(self.list, 1, wx.EXPAND)
self.list.Select(11)
self.list.EnsureVisible(11)
self.Bind(wx.EVT_CHECKBOX,self.OnCheck)
self.SetSizer(hbox)
def OnCheck(self,event):
x=self.chk_item[event.GetId()].GetText()
pass
class RowButtons(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, -1)
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.listCtrl = parent.panel1.list
#self.listCtrl = parent.GetParent().panel1.list
search_words = wx.TextCtrl
(self,id=-1,value='a',style=wx.TE_PROCESS_ENTER)
search_executable = wx.TextCtrl
(self,id=-1,value='e',style=wx.TE_PROCESS_ENTER)
search_title = wx.TextCtrl
(self,id=-1,value='i',style=wx.TE_PROCESS_ENTER)
ok_button = wx.Button(self,id=-1,label='OK')
join_button=wx.Button(self,id=-1,label='Join')
sizer.Add(search_words)
sizer.Add(search_executable)
sizer.Add(search_title)
sizer.Add(ok_button)
sizer.Add(join_button)
self.SetSizer(sizer)
sizer.Layout()
self.Bind(wx.EVT_BUTTON,self.hit_ok_button)
self.Bind
(wx.EVT_TEXT_ENTER,self.search_words_enter,search_words)
self.Bind
(wx.EVT_TEXT_ENTER,self.search_executable_enter,search_executable)
self.Bind
(wx.EVT_TEXT_ENTER,self.search_title_enter,search_title)
def hit_ok_button(self, event):
w=self.listCtrl.GetItemCount()
#x=self.listCtrl[event.GetId()]
self.listCtrl.DeleteItem(9)
pass
def search_words_enter(self,event):
self.filter_words(event.GetString(),1)
def search_executable_enter(self,event):
self.filter_words(event.GetString(),2)
def search_title_enter(self,event):
self.filter_words(event.GetString(),3)
def filter_words(self,word,col):
lc = self.listCtrl
dlist=[]
while [dlist.append(k) for k in range(lc.GetItemCount()) if word in
lc.GetItemData(k)[col]]:
lc.DeleteItem(dlist[0])
dlist=[]
lc.Refresh()
class MainFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(500, 330))
self.panel1 = Actresses(self, -1)
panel2 = RowButtons(self,-1)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.panel1,1,wx.EXPAND|wx.ALL)
vbox.Add(panel2)
self.SetSizer(vbox)
self.Show(True)
app = wx.App()
MainFrame(None, -1, 'MainFrame')
app.MainLoop()