一次响应Listctrl更改
作者:互联网
我正在使用wxPython处理表单,我想让listctrl的值列表根据另一个listctrl的选择而更改.为此,我使用链接到控制对象的EVT_LIST_ITEM_SELECTED和EVT_LIST_ITEM_DESELECTED事件的方法来调用Publisher.sendMessage.要更改的控件具有该发布者的订阅者的方法.这有效:单击第一个listctrl时,第二个刷新.
问题在于必须从数据库刷新数据,并且每次选择和取消选择都会发送一条消息.这意味着即使我仅单击一项,数据库也会被查询两次(一次用于取消选择,然后再次用于选择).如果我按住Shift键并单击以多选5个项目,则会进行5个呼叫.有什么方法可以让listctrl响应集合,而不是单个选择?
解决方法:
最好的解决方案似乎是使用带有标志的wx.CallAfter来一次执行一次后续过程:
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.list_ctrl_1 = wx.ListCtrl(self, -1, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
sizer_1.Add(self.list_ctrl_1, 1, wx.EXPAND, 0)
self.list_ctrl_1.InsertColumn(0,"1")
self.list_ctrl_1.InsertStringItem(0,"HELLO1")
self.list_ctrl_1.InsertStringItem(0,"HELLO2")
self.list_ctrl_1.InsertStringItem(0,"HELLO3")
self.list_ctrl_1.InsertStringItem(0,"HELLO4")
self.list_ctrl_1.InsertStringItem(0,"HELLO5")
self.list_ctrl_1.InsertStringItem(0,"HELLO6")
self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list_ctrl_1)
self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected, self.list_ctrl_1)
self.dirty = False
def Cleanup(self, StringToPrint):
print 'No Longer Dirty!'
self.dirty = False
def OnItemSelected(self,event):
print str(self.__class__) + " - OnItemSelected"
if not self.dirty:
self.dirty = True
wx.CallAfter(self.Cleanup)
event.Skip()
def OnItemDeselected(self,event):
print str(self.__class__) + " - OnItemDeselected"
if not self.dirty:
self.dirty = True
wx.CallAfter(self.Cleanup)
event.Skip()
if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()
标签:listctrl,wxpython,python 来源: https://codeday.me/bug/20191105/1998540.html