wxWidgets: How to catch left-click on wxListCtrl?

一曲冷凌霜 提交于 2019-12-22 12:54:20

问题


I want to add checkboxes to a wxListCtrl, and this works fine except there doesn't appear to be an EVT_LIST_ITEM_CLICK or EVT_LIST_ITEM_LEFT_CLICK event to catch when the mouse is clicked on the item so the image can be toggled. There are events for right and middle click, just not left click - which means you have to middle or right click to tick/untick the items in the list.

Does anyone know whether there is a left-click event you can use? I tried the item selected and item activated events, but these don't report the pixel location of the event, so I can't use them to figure out whether the image was the part of the item clicked.

I was basing the code on some at the the wxWidgets wiki, except they override the wxListCtrl class which I want to avoid for simplicity. I'm also aware of wxGrid and other alternate controls, but none of them are as quick as the wxListCtrl (and the wxGrid checkboxes look awful too.)

So, does anyone know how you can get the coordinates of left-clicks in a wxListCtrl?

EDIT: Sorry, wxListCtrl not wxListGrid (was thinking too hard about wxGrid...)


回答1:


This is the first time ever I hear about wxListGrid so I don't know what is it capable of. However in general you can always catch the low level mouse click events (e.g. EVT_LEFT_UP) and find the item underneath the mouse position.




回答2:


I'm confused. You should be able to just bind to EVT_LEFT_DOWN just like with any other widget. I just tried it and it worked for me. See the following example:

import wx

class MyForm(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial", size=(500,500))

    # Add a panel so it looks the correct on all platforms
    panel = wx.Panel(self, wx.ID_ANY)
    self.index = 0

    self.list_ctrl = wx.ListCtrl(panel, size=(-1,100),
                     style=wx.LC_REPORT
                     |wx.BORDER_SUNKEN
                     )
    self.list_ctrl.InsertColumn(0, 'Subject')
    self.list_ctrl.InsertColumn(1, 'Due')
    self.list_ctrl.InsertColumn(2, 'Location', width=125)
    self.list_ctrl.Bind(wx.EVT_LEFT_DOWN, self.onLeftClick)

    btn = wx.Button(panel, label="Add Line")
    btn.Bind(wx.EVT_BUTTON, self.add_line)

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
    sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
    panel.SetSizer(sizer)

def add_line(self, event):
    line = "Line %s" % self.index
    self.list_ctrl.InsertStringItem(self.index, line)
    self.list_ctrl.SetStringItem(self.index, 1, "01/19/2010")
    self.list_ctrl.SetStringItem(self.index, 2, "USA")
    self.index += 1

def onLeftClick(self, event):
    pos = event.GetPosition()
    print str(pos)

I hope that helps.


Mike Driscoll

Blog: http://blog.pythonlibrary.org




回答3:


Here are events I capture from my ListCtrl for left click and double click:

self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnListClick, self.ballotC)
self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnListDClick, self.ballotC)

You can see an example here.



来源:https://stackoverflow.com/questions/3156923/wxwidgets-how-to-catch-left-click-on-wxlistctrl

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