When deleting select item, ErrorMsg poped up: "can't retrieve information about list control item XXX". Can anyone take a look and find a way to solve it? Appreciate your help!
删除select项时,ErrorMsg弹出:“不能检索列表控件项XXX的信息”。谁能去看看,想办法解决它吗?感谢你的帮助!
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() for 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 = [] for i 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()
----------------------------------**
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * *
1 个解决方案
#1
1
You should popup the menu in another event handler for EVT_LIST_ITEM_RIGHT_CLICK
Try following code:
您应该在EVT_LIST_ITEM_RIGHT_CLICK的另一个事件处理程序中弹出菜单,并单击以下代码:
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()
for 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()
#1
1
You should popup the menu in another event handler for EVT_LIST_ITEM_RIGHT_CLICK
Try following code:
您应该在EVT_LIST_ITEM_RIGHT_CLICK的另一个事件处理程序中弹出菜单,并单击以下代码:
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()
for 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()