Can I get clickable menu item with submenu in wxPython? - python

I'm building system tray application with wxPython. It's easy to understand and works great so far, but I'm trying to get a non-standard behavior. I would like menu item to be clickable, but have a submenu at the same time. I know it's weird, but it would meet my needs the best. I want to execute default action on click, but have more options in submenu. The below code creates items just fine without submenu.
class TrackerMenuItem(wx.MenuItem):
#staticmethod
def create_and_bind(parent_menu, label, handler):
menu_item = TrackerMenuItem(parent_menu, -1, label)
parent_menu.Bind(wx.EVT_MENU, handler, id=menu_item.GetId())
parent_menu.Append(menu_item)
return menu_item
But if I try to add submenu, binding stops working, and my item is just a parent for submenu (this is just a generic code for tests)
class TrackerMenuItem(wx.MenuItem):
#staticmethod
def create_and_bind(parent_menu, label, handler):
menu_item = TrackerMenuItem(parent_menu, -1, label)
submenu = wx.Menu()
submenuitem = wx.MenuItem(submenu, wx.ID_ANY, 'Submenu Item')
submenu.Append(submenuitem)
menu_item.SetSubMenu(submenu)
parent_menu.Bind(wx.EVT_MENU, handler, id=menu_item.GetId())
parent_menu.Append(menu_item)
return menu_item
Is it possible to work it around in a reasonable manner?

Related

wxpython set Focus to wx.Menubar by pressing a key

I have a wx.menubar in my application, and I want to set focus to it by pressing a key, for example, "alt+o", and have as a result this:
I would like to know if it is possible? or if an event in wxpython to do it exists. Thank you. P.D: I have tried to use the SetFocus () method which extends from wx.Window but it doesn't work
You could use accelerators
For example:
# ...
# Menu Bar
self.menubar = wx.MenuBar()
file_menu = wx.Menu()
item = file_menu.Append(wx.ID_EXIT, "&Salir", "")
self.menubar.Append(file_menu, "Archiv&o")
self.SetMenuBar(self.menubar)
#...
You should be able to select the "Archivo" menu using "Alt + o".

Automating a wxPython main menu setup?

I am trying to find a way to condense and automate the construction of a main menu (underneath the title bar, with file, edit, help, etc.) in wxPython.
Writing out each and every menu item is direct, but I notice I repeat myself a lot, between Appending, sorting IDs, etc. Followed by other unique pits like if I want to add an icon to a specific menu, or if I have submenus and they may have submenus, etc. Without one consistent way to itemize everything, simply by adding information to maybe a list or dictionary, or a combo of the two, my wx.Frame object will get very dense.
I can't see a clean an organized way of doing that, short of a 3-dimensional array. And even then, I don't know how to organize that 3D array uniformly so every item is ready to go.
Here is what I have so far (pardon any indentation errors; it works fine on me):
class frameMain(wx.Frame):
"""The main application frame."""
def __init__(self,
parent=None,
id=-1,
title='TITLE',
pos=wx.DefaultPosition,
size=wx.Size(550, 400),
style=wx.DEFAULT_FRAME_STYLE):
"""Initialize the Main frame structure."""
wx.Frame.__init__(self, parent, id, title, pos, size, style)
self.Center()
self.CreateStatusBar()
self.buildMainMenu()
def buildMainMenu(self):
"""Creates the main menu at the top of the screen."""
MainMenu = wx.MenuBar()
# Establish menu item IDs.
menuID_File = ['exit']
menuID_Help = ['about']
menuID_ALL = [menuID_File,
menuID_Help]
# Make a dictionary of the menu item IDs.
self.menuID = {}
for eachmenu in menuID_ALL:
for eachitem in eachmenu:
self.menuID[eachitem] = wx.NewId()
# Create the menus.
MM_File = wx.Menu()
FILE = {}
MM_File.AppendSeparator()
FILE['exit'] = MM_File.Append(self.menuID['exit'],
'Exit',
'Exit application.')
self.Bind(wx.EVT_MENU, self.onExit, FILE['exit'])
MainMenu.Append(MM_File, 'File')
MM_Help = wx.Menu()
HELP = {}
MM_Help.AppendSeparator()
HELP['about'] = MM_Help.Append(self.menuID['about'],
'About',
'About the application.')
self.Bind(wx.EVT_MENU, self.onAbout, HELP['about'])
MainMenu.Append(MM_Help, 'Help')
# Install the Main Menu.
self.SetMenuBar(MainMenu)
I tried using the list-to-dictionary thing to make it so I don't need a specific index number when referring to an ID, just write in a keyword and it gets the ID. I write it once, and it's applied across the rest of the function.
Notice how I have to make a whole new variable and repeat itself, like MM_File, MM_Edit, MM_Help, and each time I do I put in similar information to append and bind. And keep in mind, some of the menus may need Separators, or have menus in menus, or I may want to use a sprite next to any of these menu items, so I'm trying to figure how to organize my arrays to do that.
What is the appropriate way to organize this into a concise system so it doesn't bloat this class?
There are several approaches you can take with this. You can put the menu generation code into a helper function if you like. Something like this should work:
def menu_helper(self, menu, menu_id, name, help, handler, sep=True):
menu_obj = wx.Menu()
if sep:
menu_obj.AppendSeparator()
menu_item = menu_obj.Append(menu_id, name, help)
self.Bind(wx.EVT_MENU, handler, menu_item)
self.MainMenu.Append(menu_obj, menu)
Here's a complete example:
import wx
class frameMain(wx.Frame):
"""The main application frame."""
def __init__(self,
parent=None,
id=-1,
title='TITLE',
pos=wx.DefaultPosition,
size=wx.Size(550, 400),
style=wx.DEFAULT_FRAME_STYLE):
"""Initialize the Main frame structure."""
wx.Frame.__init__(self, parent, id, title, pos, size, style)
self.Center()
self.CreateStatusBar()
self.buildMainMenu()
def buildMainMenu(self):
"""Creates the main menu at the top of the screen."""
self.MainMenu = wx.MenuBar()
# Establish menu item IDs.
menuID_File = 'exit'
menuID_Help = 'about'
menuID_ALL = [menuID_File,
menuID_Help]
# Make a dictionary of the menu item IDs.
self.menuID = {item: wx.NewId() for item in menuID_ALL}
# Create the menus.
self.menu_helper('File', self.menuID['exit'], 'Exit',
'Exit application', self.onExit)
self.menu_helper('Help', self.menuID['about'], 'About',
'About the application.', self.onAbout)
# Install the Main Menu.
self.SetMenuBar(self.MainMenu)
def menu_helper(self, menu, menu_id, name, help, handler, sep=True):
"""
"""
menu_obj = wx.Menu()
if sep:
menu_obj.AppendSeparator()
menu_item = menu_obj.Append(menu_id, name, help)
self.Bind(wx.EVT_MENU, handler, menu_item)
self.MainMenu.Append(menu_obj, menu)
#----------------------------------------------------------------------
def onExit(self, event):
pass
def onAbout(self, event):
pass
if __name__ == '__main__':
app = wx.App(False)
frame = frameMain()
frame.Show()
app.MainLoop()
Or you could create a class that handles all the menu creation. You could also create a config file that has all this information in it that you read to create your menu. Another alternative would be to use XRC, although I personally find that a bit limiting.

How do I select items in MenuBar (PyQt/Python)

I have created the menu bar shown below as part of a QMainWindow Class and i wanted to run another class or def when i click on 'Save as...' in the Menubar. How could i edit the code below to allow me to do that?? By The way, when i click on Quit, it works (it closes the MainWindow).
def createMenusAndToolbars(self):
fileMenu = self.menuBar().addMenu("File")
for text in (("Save As..."), ("Quit")):
action = QtGui.QAction(text, self)
if text == "Save As...":
text.clicked.connect(self.save)
if text == "Quit":
self.connect(action, QtCore.SIGNAL("triggered()"), self.close)
fileMenu.addAction(action)
def save(self):
save = SaveTest(self)
The code you posted is essentially correct, except for one obviously wrong line. Here's a simplified version which should work as you intended:
def createMenusAndToolbars(self):
fileMenu = self.menuBar().addMenu('File')
fileMenu.addAction('Save As...', self.save)
fileMenu.addAction('Quit', self.quit)
def save(self):
save = SaveTest(self)
NB: the addAction method returns the action it creates, which would allow you to set other properties, if necessary.

wxPython menu text alignment

I'm developing a wxPython GUI, and would like to right-align shortcut text. Obviously one can cludge this using tab characters, but I'd prefer to do it natively if there is a way to.
The default menu item creation is thus:
menu = wx.Menu()
item_id = 1
item_name = 'My menu item'
help_text = 'Clicking this does something interesting.'
item = menu.Append(item_id, item_name, help_text)
I'll be expanding on this with shortcuts, so if I were using tabs, it would be something like:
item_name = 'My menu item\t\tCtrl+Alt+H'
However, that involves a lot of manual \t entries to make sure everything lines up, and anytime a menu item changes name or another item is added, they would all potentially need to be updated. Is there any way around this, e.g. a class method I'm not seeing to automatically associate the keybinding to the menu item?
Edit: I know that when passing text like &My menu item, it does something automatically with the keybinding associated with the ID specified if there is a definition associated with that ID in the accelerators table, correct?
I went digging through a couple other application's code to find the answer. It turns out the default behavior with \t doesn't do what it looks like it would do (i.e. insert a tab character), but is sensibly interpreted by the toolkit as doing precisely what I wanted to do. Thus, the way to right-align a short cut is simple: create it with the text you desire, followed by \t<shortcut> (rather as I had above). In the example code I pasted above, if I wanted my shortcut to be Ctrl + T, it should therefore be thus:
menu = wx.Menu()
item_id = 1
item_name = 'My menu item\tCtrl+T'
help_text = 'Clicking this does something interesting.'
item = menu.Append(item_id, item_name, help_text)
Edit: updated the following section based on Mike Driscoll's very helpful answer.
Note that this creates the shortcut binding (wxPython picks that up), but it doesn't make it selectable using e.g. the Alt key on Windows.
You can associate the Alt key to quickly open the menu and navigate to it by using the ampersand in your item_name text, but you'll still need to associate the desired keybinding manually via the AcceleratorTable:
menu = wx.Menu()
item_id = 1
# Ctrl+T is bound to the keybinding
accelerator_table = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('T'), item_id)])
self.setAcceleratorTable(accelerator_table)
# Ctrl+T is not included, but the menu item can be accessed via Alt key
item_name = '&My menu item'
help_text = 'Clicking this does something interesting.'
item = menu.Append(item_id, item_name, help_text)
This, I imagine, would actually be the preferred pattern, as then anywhere that the item_id was referenced, the shortcut could be referenced automatically. This would also make for seamless updates.
While Chris is right about "\t" indenting the keybinding correctly in the menu, I don't really see what he means by automatically associating anything using an ampersand. The ampersand (&) DOES allow the used to type ALT to get the File menu to open and then if you type another letter that has had the ampersand applied to it, it will jump to that menu item, but the & does not connect the menu item to the accelerator table. That is done via the menu item's ID.
See the following code:
import wx
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "wx.Menu Tutorial")
self.panel = wx.Panel(self, wx.ID_ANY)
menuBar = wx.MenuBar()
fileMenu = wx.Menu()
exitId = wx.NewId()
exitMenuItem = fileMenu.Append(exitId, "&Exit/tCtrl+X",
"Exit the application")
self.Bind(wx.EVT_MENU, self.onExit, id=exitId )
menuBar.Append(fileMenu, "&File")
self.SetMenuBar(menuBar)
accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('X'), exitId )])
self.SetAcceleratorTable(accel_tbl)
#----------------------------------------------------------------------
def onExit(self, event):
""""""
self.Close()
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm().Show()
app.MainLoop()
Note that the exitId is used to create the menu item, bind the menu item to EVT_MENU and finally, it is used in the AcceleratorTable so the user can use the shortcut key.
Here are a few references that might be helpful:
http://www.blog.pythonlibrary.org/2008/07/02/wxpython-working-with-menus-toolbars-and-accelerators/
http://wiki.wxpython.org/WorkingWithMenus
http://zetcode.com/wxpython/menustoolbars/
http://www.blog.pythonlibrary.org/2010/12/02/wxpython-keyboard-shortcuts-accelerators/

PyQt4 dropdownlist with actions

I want to create a drop down list in PyQt4, that executes an action when an element is selected. Also, some options may not be available at some time. They should still be in the list, but greyed out.
I tried attaching a menu to a QToolButton, but I can not even see the menu.
How is it done?
Thanks!
Nathan
Use a popup. You can trigger a popup anywhere, using the QMenu.exec_ method and passing the point at which you want the menu to appear.
I created a button that remembered where it was clicked, and connected that to the method to create and display the popup.
class MemoryButton(QPushButton):
def __init__(self, *args, **kw):
QPushButton.__init__(self, *args, **kw)
self.last_mouse_pos = None
def mousePressEvent(self, event):
self.last_mouse_pos = event.pos()
QPushButton.mousePressEvent(self, event)
def mouseReleaseEvent(self, event):
self.last_mouse_pos = event.pos()
QPushButton.mouseReleaseEvent(self, event)
def get_last_pos(self):
if self.last_mouse_pos:
return self.mapToGlobal(self.last_mouse_pos)
else:
return None
button = MemoryButton("Click Me!")
def popup_menu():
popup = QMenu()
menu = popup.addMenu("Do Action")
def _action(check):
print "Action Clicked!"
menu.addAction("Action").triggered.connect(_action)
popup.exec_(button.get_last_pos())
button.clicked.connect(popup_menu)
QToolButton has ToolButtonPopupMode enum that controls how it handles menus and multiple actions. When set to QToolButton::MenuButtonPopup, it will display the arrow that is typical of buttons that have menu options.
To use it set the appropriate popup mode and then you can either add a menu to the QToolButton using setMenu or you can add actions using addAction. QToolButton should then respond as expected to clicks on the menu, whether Action generated or an actual QMenu.

Categories

Resources