From a1722c7b2e395aed71d3bae0529e185d9197e3ca Mon Sep 17 00:00:00 2001 From: Lauri Gates Date: Sun, 22 Feb 2026 11:43:29 +0200 Subject: [PATCH] fix(ui): patch CTkOptionMenu for Tk 9.0 compatibility In Tk 9.0, Menu.index("end") returns "" instead of raising TclError on empty menus. CustomTkinter's DropdownMenu._add_menu_commands doesn't handle this case, causing a crash when creating CTkOptionMenu widgets (e.g., the camera selector dropdown). Add a monkey-patch that guards against the empty-string return value. --- modules/ui.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/modules/ui.py b/modules/ui.py index 8cd0fe7..74681bc 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -38,6 +38,29 @@ import platform if platform.system() == "Windows": from pygrabber.dshow_graph import FilterGraph +# --- Tk 9.0 compatibility patch --- +# In Tk 9.0, Menu.index("end") returns "" instead of raising TclError +# when the menu is empty. CustomTkinter's CTkOptionMenu doesn't handle +# this, causing crashes. This patch adds the missing guard. +try: + from customtkinter.windows.widgets.core_widget_classes import DropdownMenu as _DropdownMenu + + _original_add_menu_commands = _DropdownMenu._add_menu_commands + + def _patched_add_menu_commands(self, *args, **kwargs): + try: + end_index = self._menu.index("end") + if end_index == "" or end_index is None: + return + except Exception: + pass + _original_add_menu_commands(self, *args, **kwargs) + + _DropdownMenu._add_menu_commands = _patched_add_menu_commands +except (ImportError, AttributeError): + pass # CustomTkinter version doesn't have this class path +# --- End Tk 9.0 patch --- + ROOT = None POPUP = None POPUP_LIVE = None