Skip to content

Commit 83650c0

Browse files
committed
Implement click-outside-to-close functionality in CustomDropdownMenu
- Added a method to hide the dropdown menu when clicking outside of it. - Bound the click event to the master widget to detect clicks outside the dropdown. - Unbound the click event when the dropdown is hidden to prevent unnecessary event handling.
1 parent 3316935 commit 83650c0

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

gui/widgets.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,18 @@ def show(self):
102102
self.window.wm_geometry(f"+{x}+{y}")
103103
self.window.deiconify()
104104
self.window.lift()
105-
self.window.bind("<FocusOut>", self.hide)
106105
self.window.focus_set()
106+
self.master.bind_all("<Button-1>", self._on_click_elsewhere, add=True)
107107

108108
def hide(self, event=None):
109109
self.is_open = False
110110
self.window.withdraw()
111+
self.master.unbind_all("<Button-1>")
112+
113+
def _on_click_elsewhere(self, event):
114+
if self.window.winfo_containing(event.x_root, event.y_root) is None:
115+
if self.button.winfo_containing(event.x_root, event.y_root) is None:
116+
self.hide()
111117

112118
def update_options(self, options):
113119
for widget in self.scrollable_frame.winfo_children():

0 commit comments

Comments
 (0)