I have a cascading tkinter.Menu which I create with tearoff=1. The tearoffcommand is passed two arguments:
- the name of the menu window (the window ID of the parent window?), which is a string like ".#!menu.#!menu#!menu3", and
- the name of the torn off menu window (the window ID of the new tear-off menu's root window), which is a string like ".tearoff1".
What can I do with this information? Here's what I'd like to do: By default, the torn-off menu gets placed at screen location (x=0, y=0), but I would like to place it at another (x,y) location.
How can I make this happen?
Sample code which attempts to use "nametowidget" as suggested:
import tkinter as tk
class App:
def __init__(self, root):
menubar = tk.Menu(root)
menuA = tk.Menu(menubar, tearoff=0)
menuB = tk.Menu(menubar, tearoff=0)
menuC = tk.Menu(menubar, tearoff=1, tearoffcommand=self.onTearoff)
menuC.add_command(label="Menu Row1")
menuC.add_command(label="Menu Row2")
menuC.add_command(label="Menu Row3")
menubar.add_cascade(label="MenuA", menu=menuA)
menubar.add_cascade(label="MenuB", menu=menuB)
menubar.add_cascade(label="MenuC", menu=menuC)
self.menubar = menubar
root.config(menu=menubar)
root.geometry("300x200")
def onTearoff(self, wParent, wMenu):
print("onTearoff(%s, %s)" % (wParent, wMenu)) # wMenu = ".tearoff1"
w = root.nametowidget(wMenu) # KeyError: 'tearoff1'
w.geometry("+50+50")
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()