views:

410

answers:

1

I have a CheckLabelTool in a wx.ToolBar and I want a menu to popup directly beneath it on mouse click. I'm trying to get the location of the tool so I can set the position of the menu, but everything I've tried (GetEventObject, GetPosition, etc) gives me the position of the toolbar, so consequently the menu pops under the toolbar, but very far from the associated tool. Any suggestions? I need the tool to have toggle and bitmap capability, but I'm not fixed on CheckLabelTool if there's something else that would work better.

Thanks!

+1  A: 

Read the section on the PopupMenu method on wxpython.org:

"Pops up the given menu at the specified coordinates, relative to this window, and returns control when the user has dismissed the menu. If a menu item is selected, the corresponding menu event is generated and will be processed as usual. If the default position is given then the current position of the mouse cursor will be used."

You need to bind to the EVT_MENU event of your check tool. Once the tool button is checked, you can pop the menu up. If you don't specify the location of the popup, it will use the current position of the mouse, which is what you want.

If you want the menu to pop up at a pre-determined location that is independent of the mouse, you can get the screen location of the toolbar and add an offset

Let's look at code:

[Edit: To show how to compute the position of any point on a tool, I have modified the code to compute and display various points on the tool bar once you click a tool. The menu appears on the lower right corner of the clicked button. It works for me on Windows. I'm curious to know if it doesn't behave on other platforms.]

import wx

class ViewApp(wx.App):
    def OnInit(self):
        self.frame = ToolFrame(None, -1, "Test App")    
        self.frame.Show(True)
        return True        

class MyPopupMenu(wx.Menu):
    def __init__(self, parent):
        wx.Menu.__init__(self)

        self.parent = parent

        minimize = wx.MenuItem(self, wx.NewId(), 'Minimize')
        self.AppendItem(minimize)
        self.Bind(wx.EVT_MENU, self.OnMinimize, id=minimize.GetId())

    def OnMinimize(self, event):
        self.parent.Iconize()

class ToolFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(350, 250))

        self.toolbar = self.CreateToolBar()
        self.tool_id = wx.NewId()
        for i in range(3):
            tool_id = wx.NewId()
            self.toolbar.AddCheckLabelTool(tool_id, 'Tool', wx.EmptyBitmap(10,10))
            self.toolbar.Bind(wx.EVT_MENU, self.OnTool, id=tool_id)
        self.toolbar.Realize()
        self.Centre()
        self.Show()

    def OnTool(self, event):
        if event.IsChecked():
            # Get the position of the toolbar relative to
            # the frame. This will be the upper left corner of the first tool
            bar_pos = self.toolbar.GetScreenPosition()-self.GetScreenPosition()

            # This is the position of the tool along the tool bar (1st, 2nd, 3rd, etc...)
            tool_index = self.toolbar.GetToolPos(event.GetId())

            # Get the size of the tool
            tool_size = self.toolbar.GetToolSize()

            # This is the upper left corner of the clicked tool
            upper_left_pos = (bar_pos[0]+tool_size[0]*tool_index, bar_pos[1])

            # Menu position will be in the lower right corner
            lower_right_pos = (bar_pos[0]+tool_size[0]*(tool_index+1), bar_pos[1]+tool_size[1])

            # Show upper left corner of first tool in black
            dc = wx.WindowDC(self)
            dc.SetPen(wx.Pen("BLACK", 4))
            dc.DrawCircle(bar_pos[0], bar_pos[1], 4)        

            # Show upper left corner of this tool in blue
            dc.SetPen(wx.Pen("BLUE", 4))
            dc.DrawCircle(upper_left_pos[0], upper_left_pos[1], 4)        

            # Show lower right corner of this tool in green
            dc.SetPen(wx.Pen("GREEN", 4))
            dc.DrawCircle(lower_right_pos[0], lower_right_pos[1], 4)        

            # Correct for the position of the tool bar
            menu_pos = (lower_right_pos[0]-bar_pos[0],lower_right_pos[1]-bar_pos[1])

            # Pop up the menu
            self.PopupMenu(MyPopupMenu(self), menu_pos)

if __name__ == "__main__": 
    app = ViewApp(0)
    app.MainLoop()

Parts of this code come from here.

Matt
Thanks for your reply. The problem wasn't how to open the menu, it was how to position it when I can't get the position of an individual tool in the toolbar (the "adding an offset" part of your answer). I found a workaround for this specific problem, but for the future, I'd still like to know how to get the position (in pixels) of a tool in a toolbar, or if it's even possible.
kkeogh
See edits. It will now show you the upper left corner and bottom right corner of the clicked tool. The menu will appear on the bottom right corner.
Matt