wxPython wx.EVT_LIST_ITEM_SELECTED Not Working When Same Item is Clicked Twice in a Row

独自空忆成欢 提交于 2019-12-12 02:46:12

问题


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!


回答1:


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.




回答2:


You would want to use a mouse click event rather than an item selected event; Mike Driscoll is right.



来源:https://stackoverflow.com/questions/7576067/wxpython-wx-evt-list-item-selected-not-working-when-same-item-is-clicked-twice-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!