Skip to content

Commit

Permalink
Fixed exception when printing toasts
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhozic committed Sep 12, 2024
1 parent 80ec882 commit 9672a96
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/daf_gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,17 @@ def init_event_listeners(self):
}
dpi_78, dpi_600 = dpi_scaled(78), dpi_scaled(600)

last_toast: ToastNotification = None
def trace_listener(level: daf.TraceLEVELS, message: str):
last_toast: ToastNotification = ToastNotification.last_toast
if last_toast is not None and last_toast.toplevel.winfo_exists():
nonlocal last_toast

if last_toast is None:
next_position = dpi_78
elif last_toast.toplevel is None or last_toast.toplevel.winfo_exists():
# If toplevel is None, that means that tkinter re-entered the its event loop in the
# toast.show_toast() call before that same function was able to set the toplevel attribute.
# This happens due to ttkbootstrap library providing the 'alpha' parameter to Tkinter's TopLevel class,
# which causes Tkinter to re-enter its event loop and process another after_idle(trace_listener, ...) command.
next_position = max((last_toast.position[1] + dpi_78) % dpi_600, dpi_78)
else:
next_position = dpi_78
Expand All @@ -165,10 +173,9 @@ def trace_listener(level: daf.TraceLEVELS, message: str):
position=(10, next_position, "se"),
topmost=True
)
ToastNotification.last_toast = toast
last_toast = toast
toast.show_toast()

ToastNotification.last_toast = None
evt = daf.get_global_event_ctrl()
evt.add_listener(
daf.EventID.g_trace, lambda level, message: self.win_main.after_idle(trace_listener, level, message)
Expand Down

0 comments on commit 9672a96

Please sign in to comment.