右键单击

时间:2016-08-17 14:21:56

标签: wxpython

我想有一个树形控件,我可以右键单击以创建一个弹出菜单。单击项目菜单时,将发送一个事件,其中包含树中的ItemData。我不知道该怎么做。到目前为止我只是一个简单的弹出菜单生成

# Panel

def __init__(self, ...):
    # ...
    self.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self.OnRightClick)

def OnRightClick(self, event):
    popupmenu = wx.Menu()
    entries = ['One', 'Two']
    for entry in entries:
        menuItem = popupmenu.Append(-1, entry)

如何从TreeEvent获取TreeCtrl ItemData?获得此数据后,是否需要自定义事件才能将其附加到?

编辑:很好地获取项目数据采用迂回方式。

item = event.GetItem()
itemData = self.tree.GetItemData(item).GetData()

1 个答案:

答案 0 :(得分:2)

我在irc上得到了一些帮助。一旦我得到了项目数据,我就用lambda来包装一个事件处理程序

def OnRightClick(self, event):
    """Setup and Open a popup menu."""
    # Get TreeItemData
    item = event.GetItem()
    itemData = self.tree.GetItemData(item).GetData()
    # Create menu
    popupmenu = wx.Menu()
    entries = ['One', 'Two']
    for entry in entries:
        menuItem = popupmenu.Append(-1, entry)
        wrapper = lambda event: self.OnStuff(event, itemData)
        self.Bind(wx.EVT_MENU, wrapper, menuItem)

    # Show menu
    self.PopupMenu(popupmenu, event.GetPoint())

def OnStuff(self, event, data=None):
    myEvent = events.myCustomEvent(self.GetId(), data=data)
    wx.PostEvent(self, myEvent)