Cross-post of http://stackoverflow.com/questions/28976019/open-wxpython-menu-in-menubar-programatically
I
am trying to set up modified ALT+key accelerators in wxpython that will
open the menus in the menubar, but without pressing the ALT key. Hence, pressing the ‘R’ key in my example below would in theory open the
Run menu, but everything that I have tried so far doesn’t work. (I have tried sending custom events to the menu item, to the menubar, etc.) It seems that wx.Menu classes just do not have any sort of Popup or Show methods, which would otherwise be ideal. There is of course PopupMenu, but that can’t be used for menus that are in a menubar. And menubar doesn’t have any useful methods for popping up a menu that is part of it as far as I can tell. It must have an internal method to make the menu appear on the screen, but I can’t figure out how to access it.
import wx
class MainFrame(wx.Frame):
def __init__(self, parent, *args, **kwargs):
wx.Frame.__init__(self, parent, *args, **kwargs)
self.make_menu_bar()
# INSERT MAGIC CODE HERE TO OPEN THE RUN MENU WITHOUT CLICKING ON IT
def make_menu_bar(self):
self.menuRun = wx.Menu()
self.menuRunGo = wx.MenuItem(self.menuRun, -1, "&Go", "", wx.ITEM_NORMAL)
self.menuRun.AppendItem(self.menuRunGo)
self.menuBar = wx.MenuBar()
self.menuBar.Append(self.menuRun, "&Run")
self.SetMenuBar(self.menuBar)
if __name__=='__main__':
app = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()
Ian Bell wrote:
Cross-post of
python - Open WXpython menu in menubar programmatically - Stack Overflow
I am trying to set up modified ALT+key accelerators in wxpython that
will open the menus in the menubar, but without pressing the ALT key.
Hence, pressing the 'R' key in my example below would in theory open
the Run menu, but everything that I have tried so far doesn't work.
(I have tried sending custom events to the menu item, to the menubar,
etc.) It seems that wx.Menu classes just do not have any sort of
Popup or Show methods, which would otherwise be ideal. There is of
course PopupMenu, but that can't be used for menus that are in a
menubar. And menubar doesn't have any useful methods for popping up a
menu that is part of it as far as I can tell. It must have an
internal method to make the menu appear on the screen, but I can't
figure out how to access it.
Why? I can understand wanting to take menu actions without requiring
clicks, but why open a menu? The user is not going to expect that.
···
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
While this sounds like a bad idea from the user perspective, I believe you can do this with the UIActionSimulator. I just tried recording some keystrokes using its demo in the wxPython demo package and it worked for opening the menu.
···
On Wednesday, March 11, 2015 at 9:40:21 AM UTC-5, Ian Bell wrote:
Cross-post of http://stackoverflow.com/questions/28976019/open-wxpython-menu-in-menubar-programatically
I
am trying to set up modified ALT+key accelerators in wxpython that will
open the menus in the menubar, but without pressing the ALT key. Hence, pressing the ‘R’ key in my example below would in theory open the
Run menu, but everything that I have tried so far doesn’t work. (I have tried sending custom events to the menu item, to the menubar, etc.) It seems that wx.Menu classes just do not have any sort of Popup or Show methods, which would otherwise be ideal. There is of course PopupMenu, but that can’t be used for menus that are in a menubar. And menubar doesn’t have any useful methods for popping up a menu that is part of it as far as I can tell. It must have an internal method to make the menu appear on the screen, but I can’t figure out how to access it.
import wx
class MainFrame(wx.Frame):
def __init__(self, parent, *args, **kwargs):
wx.Frame.__init__(self, parent, *args, **kwargs)
self.make_menu_bar()
# INSERT MAGIC CODE HERE TO OPEN THE RUN MENU WITHOUT CLICKING ON IT
def make_menu_bar(self):
self.menuRun = wx.Menu()
self.menuRunGo = wx.MenuItem(self.menuRun, -1, "&Go", "", wx.ITEM_NORMAL)
self.menuRun.AppendItem(self.menuRunGo)
self.menuBar = wx.MenuBar()
self.menuBar.Append(self.menuRun, "&Run")
self.SetMenuBar(self.menuBar)
if __name__=='__main__':
app = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()
wxPython provides accelerators, so you can hear the “r” event. And then just mimic keypresses that invoke the menu. When you mimic keyboard and mouse, it really can do just whatever they can do (in most cases).
You can see some python keypress code here: DragonflyRules/_keyboard.py at 7eb708c36304e37d2877b810e0ab10643fa1e445 · chajadan/DragonflyRules · GitHub
Something like sendAltKey would work for you, but if this code is for someone else’s use, I’d recommend sending all the input in a single SendInput call, because it will place it in the buffer continuously – if you’re super careful and check keystates, you can save and restore shift/alt downs that are present, and not interrupt someone who was even in the middle of typing.
···
On Wednesday, March 11, 2015 at 7:40:21 AM UTC-7, Ian Bell wrote:
Cross-post of http://stackoverflow.com/questions/28976019/open-wxpython-menu-in-menubar-programatically
I
am trying to set up modified ALT+key accelerators in wxpython that will
open the menus in the menubar, but without pressing the ALT key. Hence, pressing the ‘R’ key in my example below would in theory open the
Run menu, but everything that I have tried so far doesn’t work. (I have tried sending custom events to the menu item, to the menubar, etc.) It seems that wx.Menu classes just do not have any sort of Popup or Show methods, which would otherwise be ideal. There is of course PopupMenu, but that can’t be used for menus that are in a menubar. And menubar doesn’t have any useful methods for popping up a menu that is part of it as far as I can tell. It must have an internal method to make the menu appear on the screen, but I can’t figure out how to access it.
import wx
class MainFrame(wx.Frame):
def __init__(self, parent, *args, **kwargs):
wx.Frame.__init__(self, parent, *args, **kwargs)
self.make_menu_bar()
# INSERT MAGIC CODE HERE TO OPEN THE RUN MENU WITHOUT CLICKING ON IT
def make_menu_bar(self):
self.menuRun = wx.Menu()
self.menuRunGo = wx.MenuItem(self.menuRun, -1, "&Go", "", wx.ITEM_NORMAL)
self.menuRun.AppendItem(self.menuRunGo)
self.menuBar = wx.MenuBar()
self.menuBar.Append(self.menuRun, "&Run")
self.SetMenuBar(self.menuBar)
if __name__=='__main__':
app = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()