繁体   English   中英

Wxpython:将菜单放置在工具栏按钮下

[英]Wxpython: Positioning a menu under a toolbar button

我在wx.ToolBar中有一个CheckLabelTool,并且我希望在单击鼠标时在其下方弹出一个菜单。 我正在尝试获取工具的位置,以便可以设置菜单的位置,但是我尝试过的所有内容(GetEventObject,GetPosition等)都为我提供了工具栏的位置,因此菜单在工具栏下弹出,但与相关工具距离很远。 有什么建议么? 我需要该工具具有切换和位图功能,但是如果还有其他更好的方法,我在CheckLabelTool上并不会得到解决。

谢谢!

阅读关于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()

这段代码的一部分来自这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM