CTkMenuBar icon indicating copy to clipboard operation
CTkMenuBar copied to clipboard

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

Open adwamogus opened this issue 1 year ago • 1 comments

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:

  • Create a CTkMenuBar instance within a class that inherits from customtkinter.CTkFrame.
  • Add dropdowns using add_cascade() and define them in a class structure.
  • Run the application.
  • Attempt to trigger dropdowns by clicking the buttons.

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 avatar Sep 08 '24 12:09 adwamogus

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")

adwamogus avatar Sep 08 '24 13:09 adwamogus