[Tutor] Creating menu shortcut key bindings
Phil
phillor9 at gmail.com
Thu Jun 1 00:15:34 EDT 2023
I've fooled around with this for several hours including extensive
searching of the Internet.
What I have is a file menu with open and exit options that I've created
with pygubu, which works perfectly. Now I'm trying to add a shortcut key
(ctl + o) to open a file.
The code under 'Get a reference to the "Open" menu item widget' and
'Bind the shortcut key to the menu item' does not produce an error. So I
presume that the code under "Set the accelerator property of the menu
item to 'Control+O'" is the problem.
My AI friend has suggested several options but they all result in an
'unknown option' error message.
Can anyone see where I've gone astray?
def __init__(self, master=None):
self.builder = builder = pygubu.Builder()
builder.add_resource_path(PROJECT_PATH)
builder.add_from_file(PROJECT_UI)
# Main widget
self.mainwindow = builder.get_object("Toplevel1", master)
# Main menu
_main_menu = builder.get_object("Menu1", self.mainwindow)
self.mainwindow.configure(menu=_main_menu)
builder.connect_callbacks(self)
# Get a reference to the "Open" menu item widget
open_menu_item = builder.get_object("mnu_open", _main_menu) #
no error here
# Set the accelerator property of the menu item to 'Control+O'
# option problems here
#open_menu_item['underline'] = 0#'Control+O'
#open_menu_item['label'] = 'Open\tCtrl+O'
#open_menu_item['<Control-o>']#accelerator'] = 'Ctrl+O'
# Bind the shortcut key to the menu item
self.mainwindow.bind('<Control-o>', lambda event:
open_menu_item.invoke()) # no error here
--
Regards,
Phil
More information about the Tutor
mailing list