I’d like to bring up a pop-up menu with a preselected item. If I just call GtkMenuItem.select() on the item before popping up the menu, the item is selected as expected. However navigating the menu with the keyboard arrow keys doesn’t start at the selected item. That is, pressing the down arrow selects the first item in the menu, but I’d like it to select the one below the initial selection.
There are other problems involving deselecting the preselected item when using the mouse, so that there don’t appear to be two items selected, but I can work around that by catching enter-notify and focus change events in the other menu items (I think). I don’t know how to get the arrow keys to behave, though.
Here’s a very simple example. Press the button and use the arrow keys to move into the menu.
import gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk, Gdk def popup(button): menu = Gtk.Menu() for name in ("A", "B", "C", "D", "E"): menuitem = Gtk.MenuItem(name) menu.append(menuitem) if name == "C": menuitem.select() menuitem.grab_focus() menu.show_all() menu.popup_at_widget(button, Gdk.Gravity.SOUTH_WEST, Gdk.Gravity.NORTH_WEST, None) window = Gtk.Window(Gtk.WindowType.TOPLEVEL) window.connect('delete-event', Gtk.main_quit) button = Gtk.Button("Click me") button.connect("clicked", popup) window.add(button) window.show_all() Gtk.main()