0

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.

3
  • It sounds like you're asking how to create a menu that isn't actually a menu? If it's not a popup window. how do you expect it to appear? What do you mean by "opened in same window with menu"? Commented Jul 30, 2016 at 13:27
  • I want the submenu to not torn off. Commented Jul 30, 2016 at 13:30
  • Again, it works as charm when I do not use tearoff=0, but when I add this functionality, it does not return anything. That's weird. Commented Jul 30, 2016 at 13:32

1 Answer 1

4

I think

def f(event): submenu.post(mainmenu.winfo_rootx(), mainmenu.winfo_rooty() + mainmenu.winfo_height()) 

does what you want, even with the tearoff=False option.

Sign up to request clarification or add additional context in comments.

1 Comment

This way is the best solution so far. I had to include it in my code. I actually expect more "natural" way though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.