Wxpython:将菜单定位在工具栏按钮下
我在 wx.ToolBar 中有一个 CheckLabelTool,我希望在单击鼠标时在其正下方弹出一个菜单。 我试图获取工具的位置,以便设置菜单的位置,但我尝试过的所有操作(GetEventObject、GetPosition 等)都给了我工具栏的位置,因此菜单会在工具栏下方弹出,但距离相关工具还很远。 有什么建议么? 我需要该工具具有切换和位图功能,但如果有其他东西可以更好地工作,我不会固定在 CheckLabelTool 上。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读 wxpython.org 上有关 PopupMenu 方法的部分:
您需要绑定到您的检查工具的EVT_MENU事件。一旦工具按钮被检查,您就可以弹出菜单。如果您不指定弹出的位置,它将使用当前位置鼠标的位置,这就是你想要的
如果你想让菜单弹出在与鼠标无关的预定位置,你可以获取工具栏的屏幕位置并添加一个偏移量
让我们看一下代码:
[ 编辑:为了展示如何计算工具上任何点的位置,我修改了代码,以便在单击工具后计算并显示工具栏上的各个点菜单出现在下部。它在 Windows 上适用于我,我很好奇它在其他平台上是否不起作用。]
部分代码来自 此处。
Read the section on the PopupMenu method on wxpython.org:
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.]
Parts of this code come from here.