Shatnerz
Shatnerz

Reputation: 2543

wxPython Popupmenu on a TreeCtrl right click

I would like to have a tree control where I can right click to create a popup menu. When an item menu is clicked, an event is then sent which contains the ItemData from the tree. I have no idea how to go about this. All I have so far is a simple popup menu generation

# 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)

How can I get the TreeCtrl ItemData from the TreeEvent? Once I have this data, do I need a custom event to attach it to?

edit: Well getting the item data takes a roundabout way.

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

Upvotes: 4

Views: 2585

Answers (1)

Shatnerz
Shatnerz

Reputation: 2543

Well I got some help on irc. Once I got the item data I just used lambda to wrap one event handler

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)

Upvotes: 5

Related Questions