Tkinter Menu within menu

43 Views Asked by At

I have a application written tkinter and created menu within menu to select book categories

The problem is that I can't write category names which have subcategories to button. If there is no subcategory, it successfully write the category name on the button

It work excatly as i want in macOS, but in Windows not work

I get the categories from the json file, the file structure is as follows as an example;

{
    "Book": {
        "ID": 1,
        "SubCategory": {
            "Law Books": {
                "ID": 10
            }
            "Computer Science Books": {
                "ID": 20,
                "SubCategory": {
                    "Programming": {
                        "ID": 30
                    }
                }
            }
        }
    }
}
    self.category_button = ttk.Menubutton(self, text='Choose')
    self.category_button = self.create_categories_menu(self.category_button)

    def create_categories_menu(self, category_button):
        categories = helper.load_categories()

        top_menu = tk.Menu(category_button, tearoff=False)
        category_button.configure(menu=top_menu)

        self.create_sub_category(categories, category_button, top_menu)

        return category_button

    def create_sub_category(self, categories, category_button, top_menu):
        # create submenus as find subcategories
        for category in categories:
            if ('SubCategory' not in categories[category]):
                top_menu.add_cascade(
                    label=category,
                    command=lambda c=category:
                        self.set_button_label(category_button, c))
            else:
                new_menu = tk.Menu(top_menu, tearoff=False)
                top_menu.add_cascade(
                    label=category,
                    menu=new_menu,
                    command=lambda c=category:
                        self.set_button_label(category_button, c))

                self.create_sub_category(
                    categories[category]['SubCategory'],
                    category_button,
                    new_menu)

    def set_button_label(self, button, name):
        button['text'] = name
0

There are 0 best solutions below