Making a popup menu more context aware

I've got my Popup menu working- the problem is it's always there if I
right click.

I want to make the context menu more context-aware. That is, I only
want it available when the user
right clicks on one ListBox only. When the mouse is out of the list
box, the menu should not be available.

So I was looking through the mouse events- and I'm wondering if I bind
EVT_ENTER_WINDOW and EVT_LEAVE_WINDOW to that listbox, and setup their
event handlers to enable/disable the Popup menu, is the right way to
go.

Hi,

I've got my Popup menu working- the problem is it's always there if I
right click.

I want to make the context menu more context-aware. That is, I only
want it available when the user
right clicks on one ListBox only. When the mouse is out of the list
box, the menu should not be available.

So I was looking through the mouse events- and I'm wondering if I bind
EVT_ENTER_WINDOW and EVT_LEAVE_WINDOW to that listbox, and setup their
event handlers to enable/disable the Popup menu, is the right way to
go.

Just bind EVT_CONTEXT_MENU to the window you want to have a menu for (a ListBox in your example) and call its PopupMenu method to show the menu. Not sure how else you have this working (bound to your frame or something?)

import wx

class Frame(wx.Frame):
     def __init__(self, parent, title="Test"):
         wx.Frame.__init__(self, parent, title=title)

         panel = wx.Panel(self)
         self._lb = wx.ListBox(panel, choices = ["hello", "world"])
         self._choice = wx.Choice(panel)
         sizer = wx.BoxSizer(wx.VERTICAL)
         sizer.AddMany([(self._choice, 0, wx.EXPAND), (self._lb, 1, wx.EXPAND)])
         panel.SetSizer(sizer)

         self._lb.Bind(wx.EVT_CONTEXT_MENU, self.OnClick)

     def OnClick(self, evt):
         menu = wx.Menu(title="HELLO")
         self._lb.PopupMenu(menu)

app = wx.App(False)
frame = Frame(None)
frame.Show(True)
app.MainLoop()

Cody

ยทยทยท

On Dec 3, 2009, at 10:05 PM, cappy2112 wrote:

Just bind EVT_CONTEXT_MENU to the window you want to have a menu for
(a ListBox in your example) and call its PopupMenu method to show the
menu. Not sure how else you have this working (bound to your frame or
something?)

Yes- I started with the example in WIA p315/316 because I hadn't used
a popup menu before.
Because of this, the popup was indeed bound to the frame, and I needed
to fine-tune it a bit.

Your suggestion makes better sense though

thanks