I have created a submenu inside a menu using Menubutton and Menu widgets:
from tkinter import * root = Tk() def f(event): submenu.invoke(0) mainmenu = Menubutton(root, text="Menu") mainmenu.pack() submenu = Menu(mainmenu) mainmenu.config(menu=submenu) submenu.add_command(label="Option 1") submenu.add_command(label="Option 2") Now I want to add a key binding to my menu:
mainmenu.bind("<Key>", f) mainmenu.focus_set() It works as charm: when I press a key, it opens up the submenu. But the problem is, the submenu is opened as a torn off toplevel window. But I want it to be opened in same window with menu. I added tearoff=0 into submenu (so it became like this:submenu = Menu(mainmenu, tearoff=0)). But now, it does not return anything. I'm trying to figure out why it doesn't. Maybe I'm doing something wrong...
I have looked for a solution, but none of them solves the problem. All they tell is just adding a key binding that prints out something, however I want a key binding that kinda automatically clicks on a menu item, and it pops up the item elements, but not as a separate window (maybe it's called contextmenu?). I couldn't find any solution to this specific problem anywhere.
So how can I make it happen? Any help would be appreciated.
tearoff=0, but when I add this functionality, it does not return anything. That's weird.