From 7e24d24a37295eaf773d3167725e00036e8b73fc Mon Sep 17 00:00:00 2001 From: Alejandro Autalan Date: Thu, 5 Dec 2024 01:31:17 -0300 Subject: [PATCH] Avoid creating style on every <> event. --- tkcalendar/calendar_.py | 20 ++++++++++++++++++-- tkcalendar/dateentry.py | 10 ++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/tkcalendar/calendar_.py b/tkcalendar/calendar_.py index b3cecab..d6e3e7e 100644 --- a/tkcalendar/calendar_.py +++ b/tkcalendar/calendar_.py @@ -492,10 +492,13 @@ def __init__(self, master=None, **kw): self.config(state=state) + self._theme_name = None + self._setup_style() + # --- bindings - self.bind('<>', self._setup_style) + self.theme_change_cbid = None + self.bind('<>', self.schedule_style_update) - self._setup_style() self._display_calendar() self._btns_date_range() self._check_sel_date() @@ -746,6 +749,18 @@ def _textvariable_trace(self, *args): self._display_calendar() self._display_selection() + def schedule_style_update(self, event=None): + if self.theme_change_cbid is None: + self.theme_change_cbid = self.after(10, self._on_theme_change) + + def _on_theme_change(self): + theme = self.style.theme_use() + if self._theme_name != theme: + # the theme has changed, update the DateEntry style to look like a combobox + self._theme_name = theme + self._setup_style() + self.theme_change_cbid = None + def _setup_style(self, event=None): """Configure style.""" self.style.layout('L.%s.TButton' % self._style_prefixe, @@ -819,6 +834,7 @@ def _setup_style(self, event=None): self.style.map(self._style_prefixe + '.TLabel', background=[('disabled', dis_day_bg)], foreground=[('disabled', dis_day_fg)]) + self._theme_name = self.style.theme_use() # --- display def _display_calendar(self): diff --git a/tkcalendar/dateentry.py b/tkcalendar/dateentry.py index e6d782e..7500cc6 100644 --- a/tkcalendar/dateentry.py +++ b/tkcalendar/dateentry.py @@ -159,8 +159,8 @@ def __init__(self, master=None, **kw): # --- bindings # reconfigure style if theme changed - self.bind('<>', - lambda e: self.after(10, self._on_theme_change)) + self.theme_change_cbid = None + self.bind('<>', self.schedule_style_update) # determine new downarrow button bbox self.bind('', self._determine_downarrow_name) self.bind('', self._determine_downarrow_name) @@ -181,6 +181,10 @@ def __getitem__(self, key): def __setitem__(self, key, value): self.configure(**{key: value}) + def schedule_style_update(self, event=None): + if self.theme_change_cbid is None: + self.theme_change_cbid = self.after(10, self._on_theme_change) + def _setup_style(self, event=None): """Style configuration to make the DateEntry look like a Combobbox.""" self.style.layout('DateEntry', self.style.layout('TCombobox')) @@ -202,6 +206,7 @@ def _setup_style(self, event=None): # nothing to cancel pass self._determine_downarrow_name_after_id = self.after(10, self._determine_downarrow_name) + self._theme_name = self.style.theme_use() def _determine_downarrow_name(self, event=None): """Determine downarrow button name.""" @@ -237,6 +242,7 @@ def _on_theme_change(self): # the theme has changed, update the DateEntry style to look like a combobox self._theme_name = theme self._setup_style() + self.theme_change_cbid = None def _on_b1_press(self, event): """Trigger self.drop_down on downarrow button press and set widget state to ['pressed', 'active']."""