### Install CTkMenuBar via pip Source: https://context7.com/akascape/ctkmenubar/llms.txt Command to install the library into your Python environment. ```bash pip install CTkMenuBar ``` -------------------------------- ### Build a Full Application with CTkMenuBar Source: https://context7.com/akascape/ctkmenubar/llms.txt Demonstrates how to initialize a CTkMenuBar within a CustomTkinter application. It includes setting up file, edit, view, and help menus with submenus and command callbacks. ```python import customtkinter from CTkMenuBar import CTkMenuBar, CustomDropdownMenu class Application(customtkinter.CTk): def __init__(self): super().__init__() self.title("Text Editor") self.geometry("800x600") self.menu_bar = CTkMenuBar(master=self, bg_color=["gray95", "gray15"], height=28, padx=6) self.setup_file_menu() self.setup_edit_menu() self.setup_view_menu() self.setup_help_menu() self.text_area = customtkinter.CTkTextbox(self, width=780, height=500) self.text_area.pack(pady=20, padx=10, fill="both", expand=True) self.status = customtkinter.CTkLabel(self, text="Ready", anchor="w") self.status.pack(fill="x", padx=10, pady=5) def setup_file_menu(self): file_btn = self.menu_bar.add_cascade("File") file_menu = CustomDropdownMenu(widget=file_btn, width=180) file_menu.add_option(option="New", command=self.new_file) file_menu.add_option(option="Open...", command=self.open_file) file_menu.add_separator() file_menu.add_option(option="Save", command=self.save_file) file_menu.add_option(option="Save As...", command=self.save_as) file_menu.add_separator() export_menu = file_menu.add_submenu("Export") export_menu.add_option(option="As PDF", command=lambda: self.export("PDF")) export_menu.add_option(option="As HTML", command=lambda: self.export("HTML")) export_menu.add_option(option="As Markdown", command=lambda: self.export("MD")) file_menu.add_separator() file_menu.add_option(option="Exit", command=self.quit) def setup_edit_menu(self): edit_btn = self.menu_bar.add_cascade("Edit") edit_menu = CustomDropdownMenu(widget=edit_btn, width=160) edit_menu.add_option(option="Undo", command=lambda: self.status.configure(text="Undo")) edit_menu.add_option(option="Redo", command=lambda: self.status.configure(text="Redo")) edit_menu.add_separator() edit_menu.add_option(option="Cut", command=lambda: self.status.configure(text="Cut")) edit_menu.add_option(option="Copy", command=lambda: self.status.configure(text="Copy")) edit_menu.add_option(option="Paste", command=lambda: self.status.configure(text="Paste")) edit_menu.add_separator() edit_menu.add_option(option="Select All", command=lambda: self.text_area.tag_add("sel", "1.0", "end")) def setup_view_menu(self): view_btn = self.menu_bar.add_cascade("View") view_menu = CustomDropdownMenu(widget=view_btn, width=180) theme_menu = view_menu.add_submenu("Theme") theme_menu.add_option(option="Light", command=lambda: customtkinter.set_appearance_mode("light")) theme_menu.add_option(option="Dark", command=lambda: customtkinter.set_appearance_mode("dark")) theme_menu.add_option(option="System", command=lambda: customtkinter.set_appearance_mode("system")) view_menu.add_separator() view_menu.add_option(option="Zoom In", command=lambda: self.status.configure(text="Zoom In")) view_menu.add_option(option="Zoom Out", command=lambda: self.status.configure(text="Zoom Out")) def setup_help_menu(self): help_btn = self.menu_bar.add_cascade("Help") help_menu = CustomDropdownMenu(widget=help_btn, width=160) help_menu.add_option(option="Documentation", command=lambda: self.status.configure(text="Opening docs...")) help_menu.add_option(option="Keyboard Shortcuts", command=lambda: self.status.configure(text="Showing shortcuts")) help_menu.add_separator() help_menu.add_option(option="About", command=self.show_about) def new_file(self): self.text_area.delete("1.0", "end") self.status.configure(text="New file created") def open_file(self): self.status.configure(text="Open file dialog...") def save_file(self): self.status.configure(text="File saved") def save_as(self): self.status.configure(text="Save As dialog...") def export(self, format_type): self.status.configure(text=f"Exporting as {format_type}...") def show_about(self): self.status.configure(text="Text Editor v1.0 - Built with CTkMenuBar") if __name__ == "__main__": app = Application() app.mainloop() ``` -------------------------------- ### Configure CustomDropdownMenu with Styling Source: https://context7.com/akascape/ctkmenubar/llms.txt Shows how to instantiate a CustomDropdownMenu with specific visual properties like corner radius, border width, and custom color themes for both light and dark modes. ```python import customtkinter from CTkMenuBar import CTkMenuBar, CustomDropdownMenu root = customtkinter.CTk() menu = CTkMenuBar(master=root) file_button = menu.add_cascade("File") dropdown = CustomDropdownMenu( widget=file_button, width=180, corner_radius=8, border_width=2, bg_color=["gray95", "gray15"], font=("Helvetica", 13) ) dropdown.add_option(option="New File", command=lambda: print("Creating new file")) root.mainloop() ``` -------------------------------- ### Create a Horizontal Menu Bar with Dropdowns Source: https://context7.com/akascape/ctkmenubar/llms.txt Demonstrates how to initialize a CTkMenuBar, add cascade buttons, and attach CustomDropdownMenu instances with various options and separators. ```python import customtkinter from CTkMenuBar import CTkMenuBar, CustomDropdownMenu root = customtkinter.CTk() root.geometry("600x400") root.title("Application with Menu Bar") menu = CTkMenuBar( master=root, bg_color=["white", "black"], height=30, width=15, padx=8, pady=3 ) file_button = menu.add_cascade("File") edit_button = menu.add_cascade("Edit") file_dropdown = CustomDropdownMenu(widget=file_button) file_dropdown.add_option(option="New", command=lambda: print("Creating new file...")) file_dropdown.add_separator() file_dropdown.add_option(option="Exit", command=root.destroy) root.mainloop() ``` -------------------------------- ### Initialize CTkTitleMenu Source: https://github.com/akascape/ctkmenubar/blob/main/README.md Demonstrates how to initialize a CTkTitleMenu, which integrates a menu bar into the application's title bar. Note that this feature is supported only on Windows. ```python from CTkMenuBar import * menu = CTkTitleMenu(master=root) button = menu.add_cascade("Menu") ``` -------------------------------- ### Create Submenus and Options with CTkMenuBar Source: https://context7.com/akascape/ctkmenubar/llms.txt Demonstrates how to create nested submenus and add options with associated commands. This pattern is useful for organizing complex menu structures. ```python export_submenu = dropdown.add_submenu("Export As") export_submenu.add_option(option="PDF", command=lambda: print("Exporting as PDF")) export_submenu.add_option(option="PNG", command=lambda: print("Exporting as PNG")) export_submenu.add_option(option="SVG", command=lambda: print("Exporting as SVG")) export_submenu.add_separator() export_submenu.add_option(option="Custom Format...", command=lambda: print("Custom export")) ``` -------------------------------- ### Initialize CTkMenuBar Source: https://github.com/akascape/ctkmenubar/blob/main/README.md Demonstrates how to import and initialize a standard CTkMenuBar widget within a CustomTkinter application. ```python from CTkMenuBar import * menu = CTkMenuBar(master=root) button = menu.add_cascade("Menu") ``` -------------------------------- ### Configure Menu Bar Appearance Source: https://context7.com/akascape/ctkmenubar/llms.txt Demonstrates how to update the properties of an existing menu bar using the configure method, useful for dynamic theme switching. ```python import customtkinter from CTkMenuBar import CTkMenuBar root = customtkinter.CTk() menu = CTkMenuBar(master=root) # Update appearance menu.configure(bg_color="darkblue") root.mainloop() ``` -------------------------------- ### Configure CustomDropdownMenu Source: https://github.com/akascape/ctkmenubar/blob/main/README.md Shows how to create a dropdown menu, add options, separators, and submenus to a widget using the CustomDropdownMenu class. ```python from CTkMenuBar import * dropdown = CustomDropdownMenu(widget=button) dropdown.add_option(option="value") dropdown.add_separator() submenu = dropdown.add_submenu("submenu") submenu.add_option(option="value") ``` -------------------------------- ### Create Windows Title Bar Menu with CTkTitleMenu Source: https://context7.com/akascape/ctkmenubar/llms.txt Demonstrates how to initialize a CTkTitleMenu that integrates into the Windows title bar area. It shows adding cascade buttons and attaching CustomDropdownMenu instances to them. ```python import customtkinter from CTkMenuBar import CTkTitleMenu, CustomDropdownMenu root = customtkinter.CTk() root.geometry("600x400") root.title("My Application") menu = CTkTitleMenu( master=root, title_bar_color=0x303030, padx=10, width=15, x_offset=None, y_offset=6 ) file_btn = menu.add_cascade("File") file_menu = CustomDropdownMenu(widget=file_btn) file_menu.add_option(option="New Project", command=lambda: print("New Project")) file_menu.add_separator() file_menu.add_option(option="Exit", command=root.destroy) root.mainloop() ``` -------------------------------- ### Add Cascade Buttons with Customization Source: https://context7.com/akascape/ctkmenubar/llms.txt Shows how to add menu buttons to the bar using add_cascade, including custom styling, colors, and postcommand callbacks. ```python import customtkinter from CTkMenuBar import CTkMenuBar root = customtkinter.CTk() menu = CTkMenuBar(master=root) styled_button = menu.add_cascade( text="Edit", fg_color="blue", text_color="white" ) def before_open(): print("Menu is about to open!") button_with_callback = menu.add_cascade( text="Settings", postcommand=before_open ) root.mainloop() ``` -------------------------------- ### Create Nested Submenus with add_submenu Source: https://context7.com/akascape/ctkmenubar/llms.txt Explains how to use the add_submenu method to create hierarchical menu structures. It returns a new CustomDropdownMenu instance for further population. ```python dropdown = CustomDropdownMenu(widget=file_button) submenu = dropdown.add_submenu("Recent Files") submenu.add_option(option="File 1", command=lambda: print("Open 1")) ``` -------------------------------- ### CustomDropdownMenu Class Source: https://github.com/akascape/ctkmenubar/blob/main/README.md Documentation for the CustomDropdownMenu class, used internally by CTkMenuBar and CTkTitleMenu to create dropdown menus. ```APIDOC ## CustomDropdownMenu ### Description A common dropdown menu class used by CTkMenuBar and CTkTitleMenu. ### Usage ```python from CTkMenuBar import * # Assuming 'button' is a cascade widget dropdown = CustomDropdownMenu(widget=button) dropdown.add_option(option="value") dropdown.add_separator() submenu = dropdown.add_submenu("submenu") submenu.add_option(option="value") ``` ### Methods - **.add_option(option, command)**: Adds an option to the dropdown and attaches a command. - **.add_separator()**: Adds a separator line between options. - **.add_submenu(submenu_name)**: Adds a submenu as an option. - **.configure(*args)**: Changes dropdown menu options. ### Arguments | Parameter | Description | |-----------| ------------| | **widget** | Attaches the dropdown to the cascade widget. | | master | *Optional*. Changes the spawn window if required. | | bg_color | Sets the background color of the dropdown. | | fg_color | Sets the foreground color of the option button. | | text_color | Sets the text color. | | hover_color | Sets the hover color of the option button. | | separator_color | Changes the separator line color. | | font | Changes the font of the text. | | width | Sets the width of the dropdown. | | height | Sets the height of the dropdown. | | padx | Sets padding in the x-direction for the dropdown frame. | | pady | Sets padding in the y-direction for the dropdown frame. | | _*other frame parameters_ | Other CTk frame parameters can also be passed. | ``` -------------------------------- ### CTkTitleMenu Widget Source: https://github.com/akascape/ctkmenubar/blob/main/README.md Documentation for the CTkTitleMenu widget, which adds a menu bar to the title bar (Windows OS only). ```APIDOC ## CTkTitleMenu ### Description Creates a menu bar that integrates with the title bar of a window. This feature is only supported on Windows OS. ### Usage ```python from CTkMenuBar import * root = customtkinter.CTk() menu = CTkTitleMenu(master=root) button = menu.add_cascade("Menu") ``` ### Methods - **.add_cascade(text, ctk_button_kwargs...)**: Adds a new menu button to the menu bar. ### Arguments | Parameter | Description | |-----------| ------------| | **master** | Defines the master window (root or toplevel only). | | bg_color | Sets the background color of the menu bar. | | title_bar_color | Sets the color to the header (only works with Windows 11). Format: `0x00rrggbb`. | | width | Sets the width of the menu bar buttons. | | padx | Sets internal padding between menu bar buttons. | | x_offset | Sets the x distance from the header. | | y_offset | Sets the y distance from the header. | | postcommand | Adds a command to be executed before the dropdown spawns. | | _*other frame parameters_ | Other CTk frame parameters can also be passed. | ``` -------------------------------- ### CTkMenuBar Widget Source: https://github.com/akascape/ctkmenubar/blob/main/README.md Documentation for the CTkMenuBar widget, used to create a classic or modern menu bar with customizability. ```APIDOC ## CTkMenuBar ### Description Creates a menu bar widget for customtkinter applications. ### Usage ```python from CTkMenuBar import * root = customtkinter.CTk() menu = CTkMenuBar(master=root) button = menu.add_cascade("Menu") ``` ### Methods - **.add_cascade(text, ctk_button_args...)**: Adds a new menu button to the menu bar. - **.configure(*args)**: Updates the parameters of the menu bar. ### Arguments | Parameter | Description | |-----------| ------------| | **master** | Defines the master widget (root or frame). | | bg_color | Sets the background color of the menu bar. | | height | Sets the height of the menu bar. | | width | Sets the width of the menu bar buttons. | | padx | Sets internal padding between menu bar buttons. | | pady | Sets internal padding in the top and bottom of the menu bar. | | postcommand | Adds a command to be executed before the dropdown spawns. | | _*other frame parameters_ | Other CTk frame parameters can also be passed. | ``` -------------------------------- ### CustomDropdownMenu.configure Source: https://context7.com/akascape/ctkmenubar/llms.txt Allows updating the dropdown menu's appearance and behavior after creation, including colors, fonts, dimensions, and padding. This enables dynamic styling of the menu. ```APIDOC ## CustomDropdownMenu.configure ### Description The configure method allows updating the dropdown menu's appearance and behavior after creation, including colors, fonts, dimensions, and padding. ### Method `configure(**kwargs)` ### Endpoint N/A (This is a class method) ### Parameters `**kwargs`: Accepts keyword arguments to modify various properties of the dropdown menu. Common arguments include: - `bg_color` (str): Background color of the dropdown. - `text_color` (str): Color of the menu option text. - `hover_color` (str): Background color when hovering over an option. - `separator_color` (str): Color of the separator lines. - `border_color` (str): Color of the dropdown border. - `font` (tuple): Font configuration (e.g., `("Arial", 14, "bold")`). ### Request Example ```python import customtkinter from CTkMenuBar import CTkMenuBar, CustomDropdownMenu root = customtkinter.CTk() menu = CTkMenuBar(master=root) style_btn = menu.add_cascade("Styles") dropdown = CustomDropdownMenu(widget=style_btn) # Apply dark style dropdown.configure( bg_color="gray20", text_color="white", hover_color="gray35", separator_color="gray40", border_color="gray50" ) # Change font dropdown.configure(font=("Arial", 14, "bold")) ``` ### Response #### Success Response (N/A) This method modifies the menu in place and does not return a value. #### Response Example N/A ``` -------------------------------- ### Add Options to CustomDropdownMenu Source: https://context7.com/akascape/ctkmenubar/llms.txt Illustrates the use of the add_option method to include clickable items in a dropdown. It covers basic commands, complex function callbacks, and custom button styling. ```python dropdown = CustomDropdownMenu(widget=actions_btn) # Basic option dropdown.add_option(option="Action 1", command=lambda: print("Action 1 executed")) # Styled option dropdown.add_option( option="Styled Option", command=lambda: print("Clicked"), fg_color="green", hover_color="darkgreen" ) ``` -------------------------------- ### Configure Dropdown Appearance Source: https://context7.com/akascape/ctkmenubar/llms.txt Updates the visual style of the dropdown menu dynamically, including colors, fonts, and borders. Useful for implementing theme switching. ```python dropdown.configure( bg_color="gray20", text_color="white", hover_color="gray35", separator_color="gray40", border_color="gray50" ) ``` -------------------------------- ### Clear and Repopulate CustomDropdownMenu Source: https://context7.com/akascape/ctkmenubar/llms.txt Shows how to use the clean() method to wipe all existing items from a dropdown menu. This is useful for creating dynamic interfaces where the menu content changes based on user interaction. ```python def populate_file_menu(): dropdown.clean() dropdown.add_option(option="New", command=lambda: print("New")) dropdown.add_option(option="Open", command=lambda: print("Open")) dropdown.add_option(option="Save", command=lambda: print("Save")) print("File menu populated") def clear_menu(): dropdown.clean() print("Menu cleared") ``` -------------------------------- ### Add Separators to CustomDropdownMenu Source: https://context7.com/akascape/ctkmenubar/llms.txt Shows how to insert horizontal lines between menu items to group related functionality. Requires the CustomDropdownMenu widget. ```python dropdown.add_option(option="Undo", command=lambda: print("Undo")) dropdown.add_separator() dropdown.add_option(option="Cut", command=lambda: print("Cut")) ``` -------------------------------- ### Remove Menu Options and Submenus in CTkMenuBar Source: https://context7.com/akascape/ctkmenubar/llms.txt Demonstrates how to remove specific options or submenus from a CustomDropdownMenu instance. It utilizes the remove_option method, which returns a boolean indicating whether the target item was successfully found and removed. ```python def remove_option_1(): success = dropdown.remove_option("Removable Option 1") print(f"Remove Option 1: {'Success' if success else 'Not found'}") def remove_submenu(): success = dropdown.remove_option("Removable Submenu") print(f"Remove Submenu: {'Success' if success else 'Not found'}") ``` -------------------------------- ### CustomDropdownMenu.add_separator Source: https://context7.com/akascape/ctkmenubar/llms.txt Adds a horizontal visual separator line between menu options to group related items together. This method is useful for organizing complex menus. ```APIDOC ## CustomDropdownMenu.add_separator ### Description The add_separator method adds a horizontal visual separator line between menu options to group related items together. ### Method `add_separator()` ### Endpoint N/A (This is a class method) ### Parameters None ### Request Example ```python import customtkinter from CTkMenuBar import CTkMenuBar, CustomDropdownMenu root = customtkinter.CTk() menu = CTkMenuBar(master=root) edit_btn = menu.add_cascade("Edit") dropdown = CustomDropdownMenu(widget=edit_btn) # Add options and separators dropdown.add_option(option="Undo") dropdown.add_option(option="Redo") dropdown.add_separator() # Adds a separator line dropdown.add_option(option="Cut") dropdown.add_option(option="Copy") ``` ### Response #### Success Response (N/A) This method does not return a value. #### Response Example N/A ``` -------------------------------- ### Remove Options from CustomDropdownMenu Source: https://context7.com/akascape/ctkmenubar/llms.txt Removes an existing menu option or submenu by its string identifier. Returns a boolean indicating success. ```python dropdown.remove_option("Removable Option 1") ``` -------------------------------- ### CustomDropdownMenu.remove_option Source: https://context7.com/akascape/ctkmenubar/llms.txt Removes a menu option or submenu by its text name. It returns True if the option was found and removed, False otherwise. This is useful for dynamically managing menu content. ```APIDOC ## CustomDropdownMenu.remove_option ### Description The remove_option method removes a menu option or submenu by its text name. It returns True if the option was found and removed, False otherwise. ### Method `remove_option(option_text: str) -> bool` ### Endpoint N/A (This is a class method) ### Parameters - **option_text** (str) - Required - The exact text of the menu option or submenu to remove. ### Request Example ```python import customtkinter from CTkMenuBar import CTkMenuBar, CustomDropdownMenu root = customtkinter.CTk() menu = CTkMenuBar(master=root) dynamic_btn = menu.add_cascade("Dynamic Menu") dropdown = CustomDropdownMenu(widget=dynamic_btn) dropdown.add_option(option="Permanent Option") dropdown.add_option(option="Removable Option 1") # Remove an option removed = dropdown.remove_option("Removable Option 1") print(f"Option removed: {removed}") # Output: Option removed: True # Try to remove a non-existent option not_removed = dropdown.remove_option("Non-existent Option") print(f"Option removed: {not_removed}") # Output: Option removed: False ``` ### Response #### Success Response (bool) - **Returns** (bool) - True if the option was successfully removed, False otherwise. #### Response Example ```json true ``` ```json false ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.