如何将两个不同的EVENT绑定到wxpython中的一个ListCtrl而不会发生冲突?

时间:2015-05-06 07:33:46

标签: python wxpython

我想在wxpython中将两个事件绑定到一个ListCtrl权重。

例如,左键单击并右键单击。前者将刷新某个地方的内容,后者将创建一个PopupMenu,其中包含有关重命名的内容,设置...

我该怎么办?

我尝试了wx.EVT_LIST_ITEM_SELECTEDwx.EVT_LIST_COL_CLICK。有用!

但是,当我使用wx.EVT_LIST_ITEM_RIGHT_CLICK时,它也会触发wx.EVT_LIST_ITEM_SELECTED

那么,如何做到这一点没有冲突?谢谢!

这是我的代码!

import wx

class ListCtrlLeft(wx.ListCtrl):
    def __init__(self, parent, i):
        wx.ListCtrl.__init__(self, parent, i, style=wx.LC_REPORT | wx.LC_HRULES | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL)
        self.parent = parent
        self.Bind(wx.EVT_SIZE, self.on_size)

        self.InsertColumn(0, '')
        self.InsertStringItem(0, 'library-one')
        self.InsertStringItem(0, 'library-two')
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.on_lib_select)
        self.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.on_lib_right_click)

    def on_size(self, event):
        size = self.parent.GetSize()
        self.SetColumnWidth(0, size.x - 5)

    def on_lib_select(self, evt):
        print "Item selected"

    def on_lib_right_click(self, evt):
        print "Item right-clicked"


class Memo(wx.Frame):
    def __init__(self, parent, i, title, size):
        wx.Frame.__init__(self, parent, i, title=title, size=size)
        self._create_splitter_windows()
        self.Centre()
        self.Show(True)

    def _create_splitter_windows(self):
        horizontal_box = wx.BoxSizer(wx.HORIZONTAL)
        splitter = wx.SplitterWindow(self, -1, style=wx.SP_LIVE_UPDATE | wx.SP_NOBORDER)
        splitter.SetMinimumPaneSize(250)
        vertical_box_left = wx.BoxSizer(wx.VERTICAL)
        panel_left = wx.Panel(splitter, -1)
        panel_left_top = wx.Panel(panel_left, -1, size=(-1, 30))
        panel_left_top.SetBackgroundColour('#53728c')
        panel_left_str = wx.StaticText(panel_left_top, -1, 'Libraries', (5, 5))
        panel_left_str.SetForegroundColour('white')

        panel_left_bottom = wx.Panel(panel_left, -1, style=wx.BORDER_NONE)
        vertical_box_left_bottom = wx.BoxSizer(wx.VERTICAL)
        # Here!!!!
        list_1 = ListCtrlLeft(panel_left_bottom, -1)
        # ----------
        vertical_box_left_bottom.Add(list_1, 1, wx.EXPAND)
        panel_left_bottom.SetSizer(vertical_box_left_bottom)

        vertical_box_left.Add(panel_left_top, 0, wx.EXPAND)
        vertical_box_left.Add(panel_left_bottom, 1, wx.EXPAND)

        panel_left.SetSizer(vertical_box_left)

        # right
        vertical_box_right = wx.BoxSizer(wx.VERTICAL)
        panel_right = wx.Panel(splitter, -1)
        # ......
        panel_right.SetSizer(vertical_box_right)

        horizontal_box.Add(splitter, -1, wx.EXPAND | wx.TOP, 1)
        self.SetSizer(horizontal_box)
        splitter.SplitVertically(panel_left, panel_right, 250)

    def on_quit(self, evt):
        self.Close()
        evt.Skip()

if __name__ == "__main__":
    app = wx.App()
    Memo(None, -1, 'PyMemo', (500, 300))
    app.MainLoop()

2 个答案:

答案 0 :(得分:0)

您可以将LIST_COL_CLICK事件绑定到listcntl和事件处理程序中。

def OnColClick(self, event):
    if event.LeftDown():
        print 'left clicked!'
    elif event.RightDown():
        print 'left clicked!'

我没有检查它是否有效,但这将是我要采取的方向。

基本上我采取了解释关键事件处理的策略:http://www.blog.pythonlibrary.org/2009/08/29/wxpython-catching-key-and-char-events/

对于鼠标事件的其他方法:http://wxpython.org/docs/api/wx.MouseEvent-class.html

答案 1 :(得分:0)

我无法复制你的问题。我尝试使用Python 2.7和wxPython 2.8.12.1在Xubuntu 14.04上运行,在使用Python 2.7和wxPython 3.0.2的Windows 7上尝试,我没有问题绑定到def proxy_kernel(X,Y,K): gram_matrix = np.zeros((X.shape[0], Y.shape[0])) for i, x in enumerate(X): for j, y in enumerate(Y): gram_matrix[i, j] = K(x, y) return gram_matrix 。它没有触发多个事件。这是我的代码:

EVT_LIST_ITEM_RIGHT_CLICK