Imagine a menubutton that contains two submenus (Tree1 and Tree2). Each submenu contains two options: "Pear" and "Apple". Is there a way to know from which Tree (submenu) a "Pear" came from after someone else picked it?
import tkinter as tk
root = tk.Tk()
def F_WhatsTheTree(event):
# This should tell me the tree from which the selected fruit comes
return
# I create a menubutton with a menu inside
menubutton = tk.Menubutton(root, text="Menu")
menubutton.menu = tk.Menu(menubutton, tearoff=0)
menubutton["menu"] = menubutton.menu
# then I create two submenus
menubutton.menu.submenu1 = tk.Menu(menubutton.menu, tearoff=0)
menubutton.menu.submenu2 = tk.Menu(menubutton.menu, tearoff=0)
# add some cascades to them
menubutton.menu.add_cascade(label="Tree1", menu=menubutton.menu.submenu1)
menubutton.menu.add_cascade(label="Tree2", menu=menubutton.menu.submenu2)
# then some fruits
menubutton.menu.submenu1.add_radiobutton(label="Pear")
menubutton.menu.submenu1.add_radiobutton(label="Apple")
menubutton.menu.submenu2.add_radiobutton(label="Pear")
menubutton.menu.submenu2.add_radiobutton(label="Apple")
# I pack the whole thing
menubutton.pack()
root.bind("<Button-3>", F_WhatsTheTree)
root.mainloop()
I have tried something like this:
def F_WhatsTheTree(event):
for i in range(len(menubutton.menu.winfo_children())):
Submenu = menubutton.menu.winfo_children()[i]
for j in range(Submenu.index("end")+1):
if Submenu.index(j) == Submenu.index("active"):
#I have also tried:
#Submenu.index(tk.ACTIVE)
#Submenu.index(tk.CURRENT)
print("The damn tree is: "+Submenu)
...but I don't know how to reference the "selected" option of a menu or the "selected" status of a radiobutton.
I've been reading the documentation of both the menu and the radiobutton to try find a way to do this, but I got nothing (obviously).
Any help would be greatly appreciated.
I would recommend giving each radiobutton a distinct value.
For example:
...submenu1.add_radiobutton(label="Pear" value="submenu1.pear")
...submenu1.add_radiobutton(label="Apple", value="submenu1.apple")
...menu.submenu2.add_radiobutton(label="Pear", value="submenu2.pear")
...menu.submenu2.add_radiobutton(label="Apple", value="submenu2.apple")
Then, in the code that uses the value you can strip off everything before the period to know which menu it came from.