python - Wxpython: Couldn't retrieve information about list control item XXX -
when deleting select item, errormsg poped up: "can't retrieve information list control item xxx". can take , find way solve it? appreciate help!
import wx
data = { 0 : (u"test2", "123456", ), 1 : (u"test", "123456",), 2 : (u"doe", "156789", ), 3 : (u"john", "13455", ) } class myapp(wx.app): def oninit(self): frame = myframe(none) frame.show() self.settopwindow(frame) return true class myframe(wx.frame): def __init__(self, parent): wx.frame.__init__(self, parent, wx.newid(), size=(500, -1)) wx.frame.centeronscreen(self) self.panel = mypanel(self) class mypanel(wx.panel): def __init__(self, parent): wx.panel.__init__(self, parent) self.list = mylistctrl(self,3) self.add_button = wx.button(self, label="add") h_sizer = wx.boxsizer(wx.horizontal) h_sizer.add(self.add_button, proportion=1, flag=wx.all, border=5) v_sizer = wx.boxsizer(wx.vertical) v_sizer.add(h_sizer, proportion=0, flag=wx.expand) v_sizer.add(self.list, proportion=1, flag=wx.expand, border=5) self.setsizer(v_sizer) self.add_button.bind(wx.evt_button,self.onadd) def onadd(self, event): self.new_w = newwindow(self) self.new_w.show() class mypopupmenu(wx.menu): def __init__(self,parent, item): super(mypopupmenu,self).__init__() self.parent = parent self.item = item menuedit = wx.menuitem(self,wx.newid(), 'edit %s' % item[0]) self.appenditem(menuedit) self.bind(wx.evt_menu, self.onedit, menuedit) menudel = wx.menuitem(self,wx.newid(), 'delete %s' % item[0]) self.appenditem(menudel) self.bind(wx.evt_menu, self.ondelete, menudel) def onedit(self,e): self.parent.parent.edit_w = newwindow(self.parent.parent) self.parent.parent.edit_w.show() def ondelete(self,e): self.parent.deleteallitems() class mylistctrl(wx.listctrl): def __init__(self, parent, columns): wx.listctrl.__init__(self, parent, style=wx.lc_report) self.parent = parent self.r_mouse = 0 self.insertcolumn(0, "name") self.insertcolumn(1, "phone") self.insertcolumn(2, "address") self.setcolumnwidth(0, 200) self.bind(wx.evt_list_item_selected, self.onselect) self.bind(wx.evt_right_down, self.onrightdown) self.itemdatamap = {} self.refresh_list() def refresh_list(self): self.deleteallitems() entry in data.items(): self.append(entry[1]) self.setitemdata(entry[0],entry[0]) def onrightdown(self, event): self.r_mouse = 1 self.position = event.getposition() event.skip() def onselect(self, event): index = event.getindex() item = [] in range(3): item.append(self.getitem(itemid=index, col=i).gettext()) self.setitemstate(index, 0, wx.list_state_selected) self.setitembackgroundcolour(index, wx.colour(255,255,0)) if self.r_mouse: self.r_mouse = 0 self.popupmenu(mypopupmenu(self, item), self.position) class newwindow(wx.frame): def __init__(self, parent): wx.frame.__init__(self, parent, size=(400,250), style=wx.default_frame_style) self.parent = parent self.centeronparent() panel = wx.panel(self) def onclick(self,event): self.close() if __name__ == "__main__": app = myapp() app.mainloop()
----------------------------------**
you should popup menu in event handler evt_list_item_right_click
try following code:
import wx data = { 0 : (u"test2", "123456", ), 1 : (u"test", "123456",), 2 : (u"doe", "156789", ), 3 : (u"john", "13455", ) } class myapp(wx.app): def oninit(self): frame = myframe(none) frame.show() self.settopwindow(frame) return true class myframe(wx.frame): def __init__(self, parent): wx.frame.__init__(self, parent, wx.newid(), size=(500, -1)) wx.frame.centeronscreen(self) self.panel = mypanel(self) class mypanel(wx.panel): def __init__(self, parent): wx.panel.__init__(self, parent) self.list = mylistctrl(self,3) self.add_button = wx.button(self, label="add") h_sizer = wx.boxsizer(wx.horizontal) h_sizer.add(self.add_button, proportion=1, flag=wx.all, border=5) v_sizer = wx.boxsizer(wx.vertical) v_sizer.add(h_sizer, proportion=0, flag=wx.expand) v_sizer.add(self.list, proportion=1, flag=wx.expand, border=5) self.setsizer(v_sizer) self.add_button.bind(wx.evt_button,self.onadd) def onadd(self, event): self.new_w = newwindow(self) self.new_w.show() class mypopupmenu(wx.menu): def __init__(self,parent, item): super(mypopupmenu,self).__init__() self.parent = parent self.item = item menuedit = wx.menuitem(self,wx.newid(), 'edit %s' % item.gettext()) self.appenditem(menuedit) self.bind(wx.evt_menu, self.onedit, menuedit) menudel = wx.menuitem(self,wx.newid(), 'delete %s' % item.gettext()) self.appenditem(menudel) self.bind(wx.evt_menu, self.ondelete, menudel) def onedit(self,e): self.parent.parent.edit_w = newwindow(self.parent.parent) self.parent.parent.edit_w.show() def ondelete(self,e): self.parent.deleteallitems() class mylistctrl(wx.listctrl): def __init__(self, parent, columns): wx.listctrl.__init__(self, parent, style=wx.lc_report) self.parent = parent self.r_mouse = 0 self.insertcolumn(0, "name") self.insertcolumn(1, "phone") self.insertcolumn(2, "address") self.setcolumnwidth(0, 200) self.bind(wx.evt_list_item_selected, self.onselect) self.bind(wx.evt_list_item_right_click, self.onrightclick) self.itemdatamap = {} self.refresh_list() def onrightclick(self,evt): pos = evt.getposition() self.popupmenu(mypopupmenu(self, self.itemsel), pos) def refresh_list(self): self.deleteallitems() entry in data.items(): self.append(entry[1]) self.setitemdata(entry[0],entry[0]) def onselect(self, event): index = event.getindex() self.itemsel = self.getitem(itemid=index) class newwindow(wx.frame): def __init__(self, parent): wx.frame.__init__(self, parent, size=(400,250), style=wx.default_frame_style) self.parent = parent self.centeronparent() panel = wx.panel(self) def onclick(self,event): self.close() if __name__ == "__main__": app = myapp() app.mainloop()
Comments
Post a Comment