From a33034fa01aa2179a7074a1b4f31ca2d2f11088e Mon Sep 17 00:00:00 2001 From: dekuNukem Date: Wed, 7 Jan 2026 19:45:57 +0000 Subject: [PATCH] right click menu with helper functions --- src/duckypad_config.py | 53 +++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/duckypad_config.py b/src/duckypad_config.py index 6e0be7c..ba9b8fb 100644 --- a/src/duckypad_config.py +++ b/src/duckypad_config.py @@ -237,6 +237,31 @@ def reset_key_button_relief(): for item in key_button_list: item.config(borderwidth=1, relief="solid") +def add_right_click_menu(text_widget): + menu = Menu(text_widget, tearoff=0) + menu.add_command(label="Cut", command=lambda: text_widget.event_generate("<>")) + menu.add_command(label="Copy", command=lambda: text_widget.event_generate("<>")) + menu.add_command(label="Paste", command=lambda: text_widget.event_generate("<>")) + menu.add_separator() + menu.add_command(label="Select All", command=lambda: text_widget.event_generate("<>")) + + def show_menu(event): + try: + text_widget.get("sel.first", "sel.last") + state = "normal" + except TclError: + state = "disabled" + menu.entryconfig("Cut", state=state) + menu.entryconfig("Copy", state=state) + try: + menu.tk_popup(event.x_root, event.y_root) + finally: + menu.grab_release() + + text_widget.bind("", show_menu) + text_widget.bind("", show_menu) + text_widget.bind("", show_menu) + def ui_reset(): global selected_key profile_add_button.config(state=DISABLED) @@ -1125,7 +1150,7 @@ def edit_header_button_click(global_setting_obj): header_script_textbox = Text(header_edit_window, relief='solid', font=script_box_font, borderwidth=1, padx=2, pady=2, spacing3=5, wrap="word", undo=True) header_script_textbox.configure(font=script_box_font, tabs=(char_width * 2)) header_script_textbox.pack(side="top", fill="both", expand=True, padx=10) - + add_right_click_menu(header_script_textbox) header_script_textbox.tag_config("error_highlight", background="yellow") # Preload the existing content @@ -1684,31 +1709,7 @@ root.update() script_textbox.bind("<>", script_textbox_event) script_textbox.tag_configure("error", background="#ffff00") -context_menu = Menu(script_textbox, tearoff=0) -context_menu.add_command(label="Cut", command=lambda: script_textbox.event_generate("<>")) -context_menu.add_command(label="Copy", command=lambda: script_textbox.event_generate("<>")) -context_menu.add_command(label="Paste", command=lambda: script_textbox.event_generate("<>")) -context_menu.add_separator() -context_menu.add_command(label="Select All", command=lambda: script_textbox.event_generate("<>")) - -def show_context_menu(event): - try: - try: - script_textbox.get("sel.first", "sel.last") - state = "normal" - except TclError: - state = "disabled" - # Update state of Cut/Copy dynamically - context_menu.entryconfig("Cut", state=state) - context_menu.entryconfig("Copy", state=state) - # Display the menu at the cursor position - context_menu.tk_popup(event.x_root, event.y_root) - finally: - # Release the grab to ensure the menu closes properly after selection - context_menu.grab_release() - -script_textbox.bind("", show_context_menu) -script_textbox.bind("", show_context_menu) +add_right_click_menu(script_textbox) def on_press_rb_click(): profile_index = profile_lstbox.curselection()[0]