How do I create a popup menu

Menus don't just pull down from menu bars at the top of your frame. They can also pop up from anywhere in the frame. Most of the time, a pop-up menu is used to provide actions that are context-sensitive, and that relate to the object at the location where the user clicks. Figure 10.6 displays an example of pop-up menus in action.

Pop-up menus are created very similarly to standard menus, however, they are not attached to the menu bar. Listing 10.9 displays the code for a sample popup menu.

Listing 10.9 Code to create a pop-up menu in an arbitrary widget import wx class MyFrame(wx.Frame):

"Popup Menu Example") self.panel = p = wx.Panel(self) menu = wx.Menu() exit = menu.Append(-1, "Exit") self.Bind(wx.EVT_MENU, self.OnExit, exit)

menuBar = wx.MenuBar() menuBar.Append(menu, "Menu") self.SetMenuBar(menuBar)

wx.StaticText(p, -1,

"Right-click on the panel to show a popup menu", (25,25))

self.popupmenu = wx.Menu() <J Creating a menu I gating for text in "one two three four five".split(): <—' the menu item = self.popupmenu.Append(-1, text)

self.Bind(wx.EVT_MENU, self.OnPopupltemSelected, item) p.Bind(wx.EVT_CONTEXT_MENU, self.OnShowPopup) <1—, def OnShowPopup(self, event): pos = event.GetPosition()

Binding a show menu event

Displaying the pop-up pos = self.panel.ScreenToClient(pos) self.panel.PopupMenu(self.popupmenu, pos)

def OnPopupItemSelected(self, event):

item = self.popupmenu.FindItemById(event.GetId()) text = item.GetText()

wx.MessageBox("You selected item '%s'" % text)

def OnExit(self, event): self.Close()

if __name__ == "__main__": app = wx.PySimpleApp() frame = MyFrame() frame.Show() app.MainLoop()

The pop-up menu is created like any other menu (notice the use of a for loop to quickly create the menu items). Instead of being appended to the menu bar, it is stored in the instance variable self.popupmenu. Then the frame binds the method OnShowPopup() to the event wx.EVT_CONTEXT_MENU. The event wx.EVT_CONTEXT_ MENU is triggered by whatever the standard mechanism is for triggering a pop-up menu on the underlying operating system. Under MS Windows and GTK, the mechanism is a right mouse click, under the Mac OS, it's a control click.

When the user performs a pop-up-triggering click on the frame, the OnShow-Popup() handler method is called. The first thing this method does is determine the position to display the menu. The event position given in the wx.Context-MenuEvent instance passed to this method is stored in absolute screen coordinates, so we need to convert the coordinates to be relative to the panel which contains the pop-up, using the method ScreenToClient().

After that the pop-up menu is invoked with the method PopupMenu(menu, pos) or you can use the related method PopupMenuXY(menu, x, y). The PopupMenu function does not return until either a menu item has been selected or the menu is dismissed by pressing escape or clicking outside the menu region. If a menu item is selected, its event is processed normally (meaning that it needs to have a method bound to evt_menu), and the event processing is also completed before the PopupMenu method returns. The return value of PopupMenu is an uninteresting Boolean, so the only mechanism for responding to the selected item is the ordinary menu event mechanism.

A pop-up menu can have a title that displays at the top of the menu when it is activated. The title is manipulated with the properties wx.Menu.SetTitle(title) and wx.Menu.GetTitle().

+1 0

Post a comment