If you google this issue you're likely to see many people say "Use a listctrl!" That's a perfectly valid answer, creating a popup menu is easy with a listctrl, but a listctrl is a more complex object and might be overkill for your needs. There's got to be a way to use a popup menu on a listbox, right? Right! Let's get to it.
So, we can use the wx.ContextMenuEvent to fire a function when you right click the listbox, this event can be bound to a listbox with wx.EVT_CONTEXT_MENU, below is a test application I put together to show what happens when you try to grab the position from a ContextMenuEvent. (Hint: It doesn't work! See the screenshot below.)
#! /usr/bin/python
import wx
import wx.lib.agw.aui as aui
class myFrame(wx.Frame):
def __init__(self,parent,ID,title,position,size):
wx.Frame.__init__(self,parent,ID,title,position,size)
self.listbox = wx.ListBox(choices=[],id=-1,parent=self,style=wx.LB_EXTENDED)
self.mgr = aui.AuiManager(self)
self.mgr.AddPane(self.listbox, aui.AuiPaneInfo().Center())
for i in xrange(10):
self.listbox.Insert(str(i),0,None)
self.mgr.Update()
self.listbox.Bind(wx.EVT_CONTEXT_MENU,self.showPopupMenu)
self.createMenu()
self.Centre()
def createMenu(self):
self.menu = wx.Menu()
item1 = self.menu.Append(-1,'Item 1')
item2 = self.menu.Append(-1,'Item 2')
def showPopupMenu(self,evt):
position = evt.GetPosition()
self.PopupMenu(self.menu,position)
class WXApp(wx.App):
def OnInit(self):
frame = myFrame(None,-1,'Test App',wx.DefaultPosition,wx.Size(680,550))
frame.Show(True)
self.SetTopWindow(frame)
return True
def main():
wxobj = WXApp(False)
wxobj.MainLoop()
main()
Notice the popup menu shows up far off the point where the mouse clicked! |
def showPopupMenu(self,evt):
position = self.ScreenToClient(wx.GetMousePosition())
self.PopupMenu(self.menu,position)
This looks better! The popup menu now appears where the mouse is located at click time. |
With the above modification we're now grabbing the mouse's position directly from wx and then getting a position that's relative to the frame. With this position we can now accurately show a popup menu! I hope this helps someone.
Definitely helped me, thanks for posting it!
ReplyDeleteIt helped me. Thanks a lot
ReplyDelete