Akascape / CTkMenuBar

Modern menu/tool bar widget library for customtkinter. (extension/add-on)
Creative Commons Zero v1.0 Universal
104 stars 7 forks source link

OOP-Incompatibility with CTkMenuBar: Dropdowns Not Functional in Class-based Approach #31

Open adwamogus opened 1 week ago

adwamogus commented 1 week ago

I've encountered an issue when using the CTkMenuBar module in an object-oriented (OOP) context. The dropdown menus do not appear or function as expected when incorporated into a class-based GUI structure. This issue seems to be specific to using CTkMenuBar with OOP patterns, while in a procedural setup, the menus work correctly.

Steps to Reproduce:

Expected Behavior: Clicking on the "File" button should show the dropdown menu with options like "New."

Actual Behavior: The dropdown menu does not appear upon clicking the "File" button.

Code Example:

import customtkinter
from CTkMenuBar import *
from CTkMenuBar.dropdown_menu import _CDMSubmenuButton
from customtkinter.windows.widgets.core_widget_classes import CTkBaseClass

app = customtkinter.CTk()
app.geometry("400x240")

class Toolbar(CTkMenuBar):
    def __init__(self, master):
        super().__init__(master=master)
        self.file_button = self.add_cascade("File")
        self.file_dropdown = FileDropdown(widget=self.file_button)

class FileDropdown(CustomDropdownMenu):
    def __init__(self, widget: CTkBaseClass | _CDMSubmenuButton, master: any = None):
        super().__init__(widget, master)
        self.add_option("New")

toolbar = Toolbar(app)

app.mainloop()
adwamogus commented 1 week ago

This is my workaround for this issue:

from settings import *
import CTkMenuBar as mb # https://github.com/Akascape/CTkMenuBar

class Toolbar(): # Non Object oriented workaround
    def __init__(self, app) -> None:
        self.app = app
        self.menu = mb.CTkMenuBar(master=self.app)

        self.file_button = self.menu.add_cascade("File")
        self.file_dropdown = mb.CustomDropdownMenu(widget=self.file_button)
        self.file_dropdown.add_option("New")