python - Wxpython: Positioning a menu under a toolbar button -


I have a CheckLabelTool in wx.ToolBar and I want to popup a menu directly on a mouse click popup. I'm trying to get the position of the tool so that I can set the position of the menu, but whatever I have tried (GetEventObject, GetPosition, etc.) gives me the status of the toolbar, so the menu will pop up under the toolbar , But is far from the affiliate tool. Any suggestions? I need tools for toggling and bitmap capability, but if I'm doing something better, I have not been fixed on the checklist.

Thank you!

Read the section on the method at wxpython.org:

"Specify The menu on the coordinates pops up above this window, and when the user has dismissed the menu, returns returns on the control. If the menu item is selected, the corresponding menu event is created and it is normally If the default status is given then the current position of the mouse cursor will be used Or

You need to bind to your check tool's EVT_MENU event. After checking the tool button, you can pop up the menu. If you do not specify the location of the popup, then it will use the current state of the mouse, which you want.

If you want the menu to pop up on a pre-defined location is independent of the mouse, you can get the screen space of the toolbar and add the offset

We refer to the code:

[ edit : To calculate the position of any point on a tool to show, after clicking on a tool, Code to calculate and display different points on the tool bar Appears at the bottom right corner of the button clicked modified menu. It works for me on Windows. I want to know that it does not behave on other platforms.]

  Import wx class ViewApp (wx.App): def OnInit (self): Self.frame = ToolFrame (None, -1, "test app") self.frame.Show (true) True class MyPopupMenu (wx.Menu) return: def __init __ (self, parent): wx.Menu. __ init __ (self) self.parent = parents at least = wx.MenuItem (self, wx.NewId ('at least') self.AppendItem (at least) self.Bind (wx.EVT_MENU , Self.OnMinimize, id = minimize.GetId ()) DEF OnMinimize (self, event): self.Paren T.Iconize () Class ToolFrame (wx.Frame): def __init __ (self, parent, id, title): wx.frame .__ init __ (self, parent, id, title, size = (350, 250 )) .toolbar = self.CreateToolBar () for self.tool_id = wx.NewId () I in range (3): tool_id = wx.NewId () self.toolbar.AddCheckLabelTool (tool_id, 'tool', wx. EmptyBitmap (10, 10)) self Tollbar Bind (wx.EVT_M ENU, self.OnTool, id = tool_id) self.toolbar.Realize () self.Centre () self.Show () OnTool def (auto, event): event.IsChecked () if: # toolbar Receive the position of relative # For frame # it will first be in the top left corner of the tool bar_pos = self.toolbar.GetScreenPosition () - self.GetScreenPosition () # This is the device's position with toolbar (1, 2, 3 , Etc ...) tool_index = self.toolbar.GetToolPos (event.GetId ()) # Tools tool_size = self.toolbar.GetToolSize () # This is the clicked tool upper_left_pos upper left corner = (bar_pos [0] Get the size of + tool_size [0] * to L_Index, bar_pause [1]) # menu position in bottom right corner, bottom_right_pos = (bar_page [0] + device_size [0] * (tool_view + 1), bar_page [1] + device_size [1]) # black DC = Show the top left corner of the first device in wx.WindowDC (auto) dc.setpen (wx.Pen ("BLACK", 4)) dc.DrawCircle (bar_pos [0], bar_pos [1], 4) # of the upper tool Show left corner in blue color DCt (wx.Pen ("BLUE", 4)) dc.DrawC green D. Show the lower right corner of this device in the satpan (wx.Pen ("GREEN", 4)). Dc.DrawCircle (lower_right_pos [0], lower_write_pos [0], upper left_page [0], upper left_pause [1], 4) 1], 4) # right for the position of tool bar = menu_pos = (lower_right_pos [0] - Bar_ps [0], bottom_right_pos [1] -bar_popes [1]) Pop up the menu itself. Popup menus (Myopup menu (menu itself), Menu_pos if the __name__ == "__main__": app = see application (0) app.MainLoop ()  

come from parts of this code.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -