开发者

wxWidgets: How to catch left-click on wxListCtrl?

开发者 https://www.devze.com 2023-01-05 04:16 出处:网络
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

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 开发者_JS百科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...)


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消