Search code examples
pythondrop-down-menutkinternestedoptionmenu

Are there any Tkinter Widgets like OptionMenu that can be nested?


I am trying to have an OptionMenu-like list selector, but I would like to have nested options. I want them to act just like a nested menu (File->Save), but I need to grid it.

If this isn't possible (short of writing a custom widget), I will end up having two selectors, where the first picks the "main menu" and the second handles the "sub menu," but I'd really like to avoid doing that.


Solution

  • Am optionmenu is just a convenience function that creates a menubutton and a menu. You can do that yourself quite easily. Here's an example:

    import Tkinter as tk
    
    class Example(tk.Frame):
        def __init__(self, parent):
            tk.Frame.__init__(self, parent)
    
            items = {"one": ["a","b","c"],
                     "two": ["d","e","f"],
                     "three": ["g","h","i"]}
    
            self.the_value = tk.StringVar()
            self.the_value.set("a")
    
            self.menubutton = tk.Menubutton(self, textvariable=self.the_value, indicatoron=True)
            self.topMenu = tk.Menu(self.menubutton, tearoff=False)
            self.menubutton.configure(menu=self.topMenu)
    
            for key in sorted(items.keys()):
                menu = tk.Menu(self.topMenu)
                self.topMenu.add_cascade(label=key, menu=menu)
                for value in items[key]:
                    menu.add_radiobutton(label=value, variable = self.the_value, value=value)
    
            self.menubutton.pack()
    
    if __name__ == "__main__":
        root = tk.Tk()
        Example(root).pack(fill="both", expand=True)
        root.mainloop()