当连续单击同一项目两次时,wxPython wx.EVT_LIST_ITEM_SELECTED 不起作用
我有下面的 wxPython 面板代码,我试图在其中向用户显示所有州,但我只希望他们能够选择(或检查)马萨诸塞州或纽约州。当用户在该状态上单击一次时,这会起作用,因为 wx.EVT_LIST_ITEM_SELECTED 事件被调用,但如果您继续单击同一状态,则不会调用该事件。仅当用户单击不同的状态时才会再次调用它。我尝试使用 wx.PyEvtHandler 来捕获事件并找出当我单击某个项目时发生的事件,但我始终看到的唯一事件是鼠标左键单击事件,该事件没有 GetIndex 属性。我是否错过了一个事件,告诉我用户仍在同一个列表项上,因此没有调用 SELECTED 或 DESELECTED?非常感谢任何帮助。
import wx,wx.grid, ExampleImages #legend images
from ObjectListView import GroupListView, ColumnDefn, ObjectListView
class NEWLIST(wx.Panel):
"""Create a new information panel"""
def __init__(self, parent, INFOB):
wx.Panel.__init__(self, size=(-1, 460), parent=parent, id=wx.ID_ANY)
self.dataOlv = GroupListView(self, wx.ID_ANY, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
self.dataOlv.useExpansionColumn = True
#----------------------------------------------------------------------
myimgsdict = {}
def myImageGetter(item):
k = item.code.capitalize()
myimgsdict[item.code] = self.dataOlv.AddImages(eval("ExampleImages.get%s16Bitmap()"%k))
if item in INFOB.list.dataOlv.GetCheckedObjects() and item.code in ('MA','NY'):
return myimgsdict[item.code]
#----------------------------------------------------------------------
self.statacolumn = ColumnDefn("Country", "left", 130, "state", useInitialLetterForGroupKey=True,isSpaceFilling=True)
self.legendcolumn = ColumnDefn("" , "left", 16, "legend" , imageGetter=myImageGetter)
self.totalcolumn = ColumnDefn("Total" , "right", 70, "total")
self.inviscolumn = ColumnDefn("" , "right", 0, "region")
self.dataOlv.SetColumns([self.statecolumn, self.legendcolumn, self.totalcolumn, self.inviscolumn])
self.dataOlv.InstallCheckStateColumn(self.statecolumn)
self.dataOlv.SetSortColumn(self.dataOlv.columns[2])
#----------------------------------------------------------------------
# Create some sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(self.dataOlv, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(mainSizer)
#----------------------------------------------------------------------
def OnItemSelected(event):
"""Action for when item is seleted by the user"""
realIndex = INFOB.list.dataOlv[event.GetIndex()]
if realIndex.code not in ('MA','NY'):
self.dataOlv.Uncheck(realIndex)
#----------------------------------------------------------------------
self.dataOlv.Bind(wx.EVT_LIST_ITEM_SELECTED, OnItemSelected)
self.dataOlv.Bind(wx.EVT_LIST_ITEM_ACTIVATED, OnItemSelected)
#self.dataOlv.Bind(wx.EVT_COMMAND_LEFT_CLICK, OnItemSelected)
self.dataOlv.Bind(wx.EVT_LIST_ITEM_DESELECTED, OnItemSelected)
self.dataOlv.Bind(wx.EVT_LIST_CACHE_HINT, OnItemSelected)
谢谢!
I have the below code for a wxPython panel in which I am trying show the user all the states, but I only want them to be able to select (or check) either Massachusetts or New York. This works when the user clicks once on that state, as the wx.EVT_LIST_ITEM_SELECTED event is called, but if you keep clicking on the same state, this event is not called. It is only called again if the user clicks on a different state. I have tried using a wx.PyEvtHandler to capture the events and find out which ones happen when I click on an item, but the only one I consistently see is the left mouse click event, which doesn't have a GetIndex attribute. Am I missing an event that is telling me that the user is still on the same list item and therefore is not calling either SELECTED or DESELECTED? Any help is greatly appreciated.
import wx,wx.grid, ExampleImages #legend images
from ObjectListView import GroupListView, ColumnDefn, ObjectListView
class NEWLIST(wx.Panel):
"""Create a new information panel"""
def __init__(self, parent, INFOB):
wx.Panel.__init__(self, size=(-1, 460), parent=parent, id=wx.ID_ANY)
self.dataOlv = GroupListView(self, wx.ID_ANY, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
self.dataOlv.useExpansionColumn = True
#----------------------------------------------------------------------
myimgsdict = {}
def myImageGetter(item):
k = item.code.capitalize()
myimgsdict[item.code] = self.dataOlv.AddImages(eval("ExampleImages.get%s16Bitmap()"%k))
if item in INFOB.list.dataOlv.GetCheckedObjects() and item.code in ('MA','NY'):
return myimgsdict[item.code]
#----------------------------------------------------------------------
self.statacolumn = ColumnDefn("Country", "left", 130, "state", useInitialLetterForGroupKey=True,isSpaceFilling=True)
self.legendcolumn = ColumnDefn("" , "left", 16, "legend" , imageGetter=myImageGetter)
self.totalcolumn = ColumnDefn("Total" , "right", 70, "total")
self.inviscolumn = ColumnDefn("" , "right", 0, "region")
self.dataOlv.SetColumns([self.statecolumn, self.legendcolumn, self.totalcolumn, self.inviscolumn])
self.dataOlv.InstallCheckStateColumn(self.statecolumn)
self.dataOlv.SetSortColumn(self.dataOlv.columns[2])
#----------------------------------------------------------------------
# Create some sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(self.dataOlv, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(mainSizer)
#----------------------------------------------------------------------
def OnItemSelected(event):
"""Action for when item is seleted by the user"""
realIndex = INFOB.list.dataOlv[event.GetIndex()]
if realIndex.code not in ('MA','NY'):
self.dataOlv.Uncheck(realIndex)
#----------------------------------------------------------------------
self.dataOlv.Bind(wx.EVT_LIST_ITEM_SELECTED, OnItemSelected)
self.dataOlv.Bind(wx.EVT_LIST_ITEM_ACTIVATED, OnItemSelected)
#self.dataOlv.Bind(wx.EVT_COMMAND_LEFT_CLICK, OnItemSelected)
self.dataOlv.Bind(wx.EVT_LIST_ITEM_DESELECTED, OnItemSelected)
self.dataOlv.Bind(wx.EVT_LIST_CACHE_HINT, OnItemSelected)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
EVT_LIST_ITEM_SELECTED 仅在首次选择项目时触发。您无法选择已选择的项目。您可以绑定到 EVT_LEFT_DOWN 并抓取鼠标的位置。然后在处理程序中,您将使用 myListCtrl.FindItemAtPos() 并将位置传递给它。类似的东西应该有效。
EVT_LIST_ITEM_SELECTED is only fired when the item is first selected. You can't select an already selected item. You could bind to EVT_LEFT_DOWN and grab the position of the mouse. Then in the handler, you would use myListCtrl.FindItemAtPos() and pass the position to it. Something like that should work.
您可能希望使用鼠标单击事件而不是项目选择事件;迈克·德里斯科尔是对的。
You would want to use a mouse click event rather than an item selected event; Mike Driscoll is right.