App doesn't put Ampersands in Menu Items (wxPython) - python

The problem is simple but i still coudn't figure it out, when i run the code, the Ampersands don't appear in Menu Items. Why?!
I'm using Python 3.7.7 and wxPython 4.1.0, on Thonny v3.2.7.
import wx
def OnQuit(e):
frame.Close()
app = wx.App()
frame = wx.Frame(None, -1, "wxPython Menu")
frame.SetSize(400, 300)
submenu1 = wx.Menu()
submenu1.Append(121, "Import Newsfeed List")
submenu1.Append(122, "Import Bookmarks")
submenu1.Append(123, "Import Mail")
menu1 = wx.Menu()
menu1.Append(10, "Open")
menu1.Append(11, "Save")
menu1.AppendSeparator()
menu1.AppendSubMenu(submenu1, "Import")
menu1.Append(13, "No Exit")
menu2 = wx.Menu()
menu2.Append(20, "Cut")
menu2.Append(21, "Copy")
menu2.Append(22, "Paste")
menu2.Enable(20, False)
menu3 = wx.Menu()
item = wx.MenuItem(menu3, 30, "&Quit\tCtrl+Q")
item.SetBitmap(wx.Bitmap("quit.png"))
menu3.Append(item)
menu3.Bind(wx.EVT_MENU, OnQuit, id = 30)
menuBar = wx.MenuBar()
menuBar.Append(menu1, "&File")
menuBar.Append(menu2, "&Edit")
menuBar.Append(menu3, "&Extras")
frame.SetMenuBar(menuBar)
frame.Centre()
frame.Show()
app.MainLoop()

SetItemLabel(self, label)
Sets the label associated with the menu item.
Note that if the ID of this menu item corresponds to a stock ID, then it is not necessary to specify a label: wxWidgets will automatically use the stock item > label associated with that ID. See the constructor for more info.
The label string for the normal menu items (not separators) may include the
accelerator which can be used to activate the menu item from keyboard. An accelerator key can be specified using the ampersand & character. In order to embed an ampersand character in the menu item text, the ampersand must be doubled. **
https://docs.wxpython.org/wx.MenuItem.html#wx-menuitem
The ampersand (&) is traditionally used as an accelerator on menu items.
If you press the ALT button your menus will (if defined with accelerators i.e. &File where the & next to F means pressing F will open the File menu) display with the accelerator letter underlined.
Press F and the File menu opens.
If menu items in the File menu have accelerators, they too will have a letter underlined, pressing that letter will select that menu item.
If you require an actual ampersand (&) without special meaning e.g. Close & Quit, you need to double the ampersand i.e. the items label should created as Close && Quit or &Close && Quit if the menu should not only display as Close & Quit but also respond when the user presses the letter C.

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".

GUI Automation of policy change for logoff button. pywinauto.findwindows.ElementNotFoundError: error. How to switch context?

I am automating the steps to change the policy for the logoff button. The steps involved are:
Open Local Group Policy using gpedit.msc
Select "Start Menu and Taskbar" from the dropdown in User Configuration > Administrative Template from the left pane
In the right pane, double click on "Change Start Menu power button"
Select the radio button "Enabled"
From the dropdown menu of options: Select "Log Off"
I have gotten through the third step, but I have a problem in mapping the "Change Start Menu Power Button" from the second step. My code is as below:
from pywinauto import Application
Application().start(r'mmc gpedit.msc')
app = Application(backend="uia").connect(path='mmc.exe')
#app.LocalGroupPolicyEditor.dump_tree()
Admin_template = app.LocalGroupPolicyEditor.child_window(title="User
Configuration", control_type="TreeItem").child_window(title="Administrative
Templates", control_type="TreeItem") # since there are same templates
Admin_template.double_click_input() # it expands the subtree
#Admin_template.dump_tree()
Start_menu = Admin_template.child_window(title="Start Menu and Taskbar",
control_type="TreeItem").double_click_input()
Start_menu.dump_tree()
#Admin_template.child_window(title="Start Menu and Taskbar",
control_type="TreeItem").dump_tree()
#Change_start_menu = Start_menu.child_window(title="Change Start Menu power
#button", control_type="MenuItem").double_click_input()
#Change_start_menu.dump_tree()
I have trouble in finding and mapping the elements in the right pane. Also, when I use Start_menu.dump_tree(), there are only "Notification" elements shown. However, the rest, which includes "Change Start Menu power button," is what I'll be double clicking next.
I appreciate the help. Thanks.
It was kinda tricky, but this should do the job (it does all the steps you've listed - presses OK - and closes the program):
import pywinauto
pywinauto.Application().start(r'mmc gpedit.msc')
app = pywinauto.Application(backend="uia").connect(path='mmc.exe')
admin_template = app.LocalGroupPolicyEditor.child_window(title="User Configuration", control_type="TreeItem").child_window(title="Administrative Templates", control_type="TreeItem")
admin_template.double_click_input()
start_menu = admin_template.child_window(title="Start Menu and Taskbar", control_type="TreeItem")
start_menu.double_click_input()
option_list = app.LocalGroupPolicyEditor.child_window(auto_id="12786", control_type="List")
# Just select any of the first options to change the focus to the list.
first_elem = option_list.child_window(title="Add Search Internet link to Start Menu", control_type="ListItem")
first_elem.click_input()
# Used to scroll down the window so that the wanted option becomes visible.
pywinauto.keyboard.send_keys("cccc")
option = option_list.child_window(title="Change Start Menu power button", control_type="ListItem")
option.double_click_input()
pop_up = app.LocalGroupPolicyEditor.child_window(auto_id="tableLayoutFullForm", control_type="Pane")
radio = pop_up.child_window(title="Enabled", auto_id="radioButtonEnabled", control_type="RadioButton")
radio.click_input()
drop_down = pop_up.child_window(title="Choose one of the following actions", auto_id="dropDownListChoose one of the following actions", control_type="ComboBox")
drop_down.click_input()
# 'Hack' to first select the Restart option and then the next option after that which starts with l (=Log off).
# This ensures that the correct setting gets set despite of what the setting was before.
pywinauto.keyboard.send_keys("rl{ENTER}")
ok = pop_up.child_window(title="OK", auto_id="buttonOK", control_type="Button")
ok.click_input()
app.kill()
Make sure to run this script as administrator or else it will fail.
Feel free to ask if you got any questions about the code :)
Edit:
If you're running a version of pywinauto <0.6.0, you'll have to replace the two occurances (lines 19 and 34) of pywinauto.keyboard.send_keys() with:
pywinauto.SendKeysCtypes.SendKeys()
If that doesn't work you could try:
pywinauto.keyboard.SendKeys()

How do I programmatically pull down a wx.Menu in wxPython

If I have a wx.Menu (in a wx.MenuBar, at the top of a frame, like normal) - how can I cause that menu to drop down and take focus, without clicking on it. I want the behavior to be as if the user had pressed the keyboard accelerator shortcut for that menu (so Alt+F for example, for the &File menu)
Try with wx.PostEvent:
event = wx.MenuEvent(wx.wxEVT_LEFT_DOWN, menuitem.GetId(), menu)
wx.PostEvent(frame, event)
Other wx mouse events: http://www.wxpython.org/docs/api/wx.MouseEvent-class.html
Found in google groups thread
I had the same requirement and found the simple way of using the PopupMenu function. It is not called from the menu object but from the parent of the menu (the window, frame, etc..)
To make sure that the menu appears at a specific position, regardless of your mouse, supply to the PopupMenu function a position parameter as well.
In the example bellow, I turned a platebtn that was opening the menu only when clicked in the right side, in the small area of the down arrow, into a button that opens the same menu in the same way when you click it anywhere on its surface.
Example:
import wx
import wx.lib.platebtn as platebtn
class MyFrame(wx.Frame):
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title, size=(300, 250))
wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
droparrow = platebtn.PB_STYLE_DROPARROW | platebtn.PB_STYLE_SQUARE | platebtn.PB_STYLE_GRADIENT
self.btn1 = platebtn.PlateButton(self, wx.ID_ANY, label=" File ", style=droparrow)
self.btn1.SetPressColor(wx.LIGHT_GREY)
self.menu1 = wx.Menu()
self.menu1.Append(1, "New")
self.menu1.Append(2, "Open")
self.menu1.Append(3, "Exit")
sm = wx.Menu()
sm.Append(8, "sub item 1")
sm.Append(9, "sub item 1")
self.menu1.AppendMenu(7, "Test Submenu", sm)
self.btn1.SetMenu(self.menu1)
self.Bind(wx.EVT_BUTTON, self.OnFile, self.btn1)
def OnFile(self, event):
self.btn1.PopupMenu(self.menu1, pos=(1, self.btn1.GetSize()[1]))
app = wx.App(False)
frame = MyFrame(None, -1, "PopupMenu example")
frame.Show()
app.MainLoop()
To define accelerators for menu in your program,Understand through the given example
example:
file_menu=wx.Menu()
menubar=wx.MenuBar()
menubar.Append(file_menu,"&File")
self.SetMenuBar(menubar)
Now we can access the File menu (here) by pressing ALT+F.
If we have other menus too,on pressing ALT ,it will point to the first Letter of menu bar,from which you can press the next key according to the name of menu_item.

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/

How to add keyboard navigation to a menu?

How can I add keyboard navigation (using Alt with underlines to suggest what other keys to use) to a python gtk gobject-introspection application.
This code works for showing a simple menu but does not add keyboard navigation:
mb = Gtk.MenuBar()
filemenu = Gtk.Menu()
filem = Gtk.MenuItem()
filem.set_label("File")
filem.set_submenu(filemenu)
closem = Gtk.MenuItem()
closem.show()
closem.set_label("Close")
closem.connect("activate", Gtk.main_quit)
filemenu.append(closem)
mb.append(filem)
How can I change it to allow keyboard navigation?
Set the use-underline property and prepend _ to the key you want to use as shortcut.
close_menu = Gtk.MenuItem()
close_menu.set_label("_Close")
close_menu.set_use_underline(True)
If your version of PyGObject is recent enough, you can also use
close_menu = Gtk.MenuItem("_Close", True)

Categories

Resources