Skip to content

Commit 5bdf92b

Browse files
committed
Fix Windows title menu lifecycle bugs and preserve host Configure bindings
1 parent 75f7400 commit 5bdf92b

1 file changed

Lines changed: 47 additions & 6 deletions

File tree

CTkMenuBar/title_menu_win.py

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def __init__(
7171

7272
self.after(10)
7373
self.master = master
74+
self._master_bind_ids = {}
7475
master_type = self.master.winfo_name()
7576

7677
if master_type=="tk":
@@ -118,18 +119,49 @@ def __init__(
118119

119120
self.padding = padx
120121

121-
self.master.bind("<Configure>", lambda _: self.change_dimension())
122-
self.master.bind("<Destroy>", lambda _: self.destroy_window() if not self.master.winfo_viewable() else None)
122+
self._master_bind_ids["<Configure>"] = self.master.bind("<Configure>", self._on_master_configure, add="+")
123+
self._master_bind_ids["<Destroy>"] = self.master.bind("<Destroy>", self._on_master_destroy, add="+")
123124
self.num = 0
124-
125-
self.master.bind("<Map>", lambda e: self.withdraw)
125+
self._master_bind_ids["<Map>"] = self.master.bind("<Map>", self._on_master_map, add="+")
126+
127+
def _on_master_configure(self, _event=None):
128+
self.change_dimension()
129+
130+
def _on_master_destroy(self, _event=None):
131+
try:
132+
if not self.master.winfo_viewable():
133+
self.destroy_window()
134+
except tk.TclError:
135+
self.destroy_window()
136+
137+
def _on_master_map(self, _event=None):
138+
try:
139+
self.withdraw()
140+
except tk.TclError:
141+
pass
142+
143+
def _unbind_master_events(self):
144+
for sequence, bind_id in self._master_bind_ids.items():
145+
try:
146+
self.master.unbind(sequence, bind_id)
147+
except (tk.TclError, AttributeError):
148+
pass
149+
self._master_bind_ids.clear()
126150

127151
def destroy_window(self):
128152
"""
129153
Destroy the title menu window.
130154
"""
155+
self._unbind_master_events()
156+
if not self.winfo_exists():
157+
return
131158
super().destroy()
132159

160+
def destroy(self):
161+
self._unbind_master_events()
162+
if self.winfo_exists():
163+
super().destroy()
164+
133165
def _set_appearance_mode(self, mode_string):
134166
"""
135167
Update the title bar color based on the current appearance mode.
@@ -205,6 +237,12 @@ def change_dimension(self):
205237
- Minimized ("iconic"): Hides the menu
206238
- Too small: Hides the menu if width becomes negative
207239
"""
240+
try:
241+
if not self.winfo_exists() or not self.master.winfo_exists():
242+
return
243+
except tk.TclError:
244+
return
245+
208246
width = self.master.winfo_width()-130-self.x_offset
209247
if width<0:
210248
self.withdraw()
@@ -218,8 +256,11 @@ def change_dimension(self):
218256
if self.master.state()=="zoomed":
219257
y += 4
220258
x -= 7
221-
self.geometry(f"{width}x{height}+{x}+{y}")
222-
self.deiconify()
259+
try:
260+
self.geometry(f"{width}x{height}+{x}+{y}")
261+
self.deiconify()
262+
except tk.TclError:
263+
return
223264

224265
def change_header_color(self, caption_color):
225266
"""

0 commit comments

Comments
 (0)