【问题标题】:Wxpython: Positioning a menu under a toolbar buttonWxpython:在工具栏按钮下定位菜单
【发布时间】:2010-11-13 12:06:22
【问题描述】:

我在 wx.ToolBar 中有一个 CheckLabelTool,我希望在单击鼠标时直接在其下方弹出一个菜单。我正在尝试获取工具的位置,以便设置菜单的位置,但是我尝试过的所有操作(GetEventObject、GetPosition 等)都为我提供了工具栏的位置,因此菜单会在工具栏下方弹出,但与相关工具相去甚远。有什么建议?我需要该工具具有切换和位图功能,但如果有其他更好的工具,我不会固定在 CheckLabelTool 上。

谢谢!

【问题讨论】:

    标签: python menu toolbar wxwidgets


    【解决方案1】:

    阅读 wxpython.org 上关于 PopupMenu 方法的部分:

    "在 指定的坐标,相对于 此窗口,并在何时返回控制 用户已关闭菜单。如果一个 菜单项被选中时, 生成相应的菜单事件 并将照常处理。如果 然后给出默认位置 鼠标光标的当前位置 将被使用。”

    您需要绑定到检查工具的 EVT_MENU 事件。选中工具按钮后,您可以弹出菜单。如果你不指定弹窗的位置,它会使用鼠标的当前位置,这就是你想要的。

    如果你想让菜单在预先确定的位置弹出,与鼠标无关,可以获取工具栏的屏幕位置并添加偏移量

    我们来看代码:

    [编辑:为了展示如何计算工具上任意点的位置,我修改了代码,以便在单击工具后计算和显示工具栏上的各个点。菜单出现在单击按钮的右下角。它适用于我在 Windows 上。我很想知道它是否在其他平台上不起作用。]

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

    部分代码来自here

    【讨论】:

    • 感谢您的回复。问题不在于如何打开菜单,而在于当我无法在工具栏中获得单个工具的位置时如何定位它(答案的“添加偏移量”部分)。我找到了解决这个特定问题的方法,但对于未来,我仍然想知道如何获取工具栏中工具的位置(以像素为单位),或者是否有可能。
    • 查看编辑。它现在将显示单击工具的左上角和右下角。菜单将出现在右下角。
    • 使用 gnome 在 Mint Linux 中运行良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多