From 362fcbaf13c3ee2704bc32d404dccbed22697b38 Mon Sep 17 00:00:00 2001 From: Tom Schimansky Date: Fri, 14 Oct 2022 19:26:16 +0200 Subject: [PATCH] added additional force_focus call for CTk and CTkToplevel on macOS to fix bug where window disappears when window is selected from Mission Control --- customtkinter/windows/ctk_tk.py | 6 ++++++ customtkinter/windows/ctk_toplevel.py | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/customtkinter/windows/ctk_tk.py b/customtkinter/windows/ctk_tk.py index 98777a2..599f18f 100644 --- a/customtkinter/windows/ctk_tk.py +++ b/customtkinter/windows/ctk_tk.py @@ -67,9 +67,15 @@ class CTk(tkinter.Tk): self._windows_set_titlebar_color("light") self.bind('', self._update_dimensions_event) + self.bind('', self._focus_in_event) self._block_update_dimensions_event = False + def _focus_in_event(self, event): + # sometimes window looses focus on macOS if window is selected from Mission Control, so focus has to be forced again + if sys.platform == "darwin": + self.focus_force() + def _update_dimensions_event(self, event=None): if not self._block_update_dimensions_event: detected_width = self.winfo_width() # detect current window size diff --git a/customtkinter/windows/ctk_toplevel.py b/customtkinter/windows/ctk_toplevel.py index fdda6c6..54bab28 100644 --- a/customtkinter/windows/ctk_toplevel.py +++ b/customtkinter/windows/ctk_toplevel.py @@ -63,6 +63,12 @@ class CTkToplevel(tkinter.Toplevel): self._windows_set_titlebar_color("light") self.bind('', self._update_dimensions_event) + self.bind('', self._focus_in_event) + + def _focus_in_event(self, event): + # sometimes window looses focus on macOS if window is selected from Mission Control, so focus has to be forced again + if sys.platform == "darwin": + self.focus_force() def _update_dimensions_event(self, event=None): detected_width = self.winfo_width() # detect current window size