I know that menu.tk_popup() can be used to open a context menu at a certain coordinate, but don't know how to open a submenu out of it too, if that makes sense. This is the code I made:
import tkinter as tk
root = tk.Tk()
root.geometry("500x400")
def contextMenu(e, openCascade=False):
my_menu2 = tk.Menu(root, tearoff=False)
my_menu2.add_command(label="command2")
my_menu = tk.Menu(root, tearoff=False)
my_menu.add_cascade(label="cascade1", menu=my_menu2)
my_menu.add_command(label="command1")
my_menu.tk_popup(e.x_root, e.y_root)
if openCascade:
my_menu2.tk_popup(e.x_root, e.y_root) #doesn't work
root.bind("<Button-3>", contextMenu)
root.bind("<Button-2>", lambda e: contextMenu(e=e, openCascade=True))
root.mainloop()
The code basically makes a window that when right-clicked (< Button-3> bind) will display the first menu (my_menu) which has a cascade (cascade1) which when manually runned (i.e. clicked), displays a submenu (my_menu2) as shown below.
The problem with this is not the right-clicking, but the middle-clicking (< Button-2> bind) does not work how I intended. When I middle-click, I tried to make it so it displays both the menus (my_menu, my_menu2), but my attempt just displays both but with the first menu overlapping, so the other doesn't show.
So the question is, how do I make it so when middle-clicking, it opens the first menu AND then automatically runs the cascade, as if it was clicked? If you do not understand what I explained please do not hesitate to ask.