From dbcbd155355f89641a5158b291e6690cd2065d34 Mon Sep 17 00:00:00 2001 From: peterhinch Date: Thu, 1 Jun 2023 08:47:19 +0100 Subject: [PATCH] Extras: simplify calendar widget. --- extras/widgets/calendar.py | 72 +++++++++++++------------------------- 1 file changed, 25 insertions(+), 47 deletions(-) diff --git a/extras/widgets/calendar.py b/extras/widgets/calendar.py index 3663935..ae61411 100644 --- a/extras/widgets/calendar.py +++ b/extras/widgets/calendar.py @@ -22,6 +22,8 @@ class Calendar: self.date.callback = self.show rows = 6 cols = 7 + self.ncells = cols * (rows - 1) # Row 0 has day labels + self.last_cell = cols * rows lw = (colwidth + 4) * cols # Label width = width of grid kwargs = {"align": ALIGN_CENTER, "fgcolor": fgcolor, "bgcolor": bgcolor} self.lbl = Label(wri, row, col, lw, **kwargs) @@ -31,53 +33,29 @@ class Calendar: self.grid[0, 0:7] = iter([d[:3] for d in DateCal.days]) # 3-char day names self.show() - def show(self): - def cell(): # Populate dict for a cell - d["fgcolor"] = self.fgcolor - d["bgcolor"] = self.bgcolor - if cur.year == today.year and cur.month == today.month and mday == today.mday: # Today - if self.today_inv: - d["fgcolor"] = self.bgcolor - d["bgcolor"] = self.today_c - else: - d["fgcolor"] = self.today_c - elif mday == cur.mday: # Currency - if self.cur_inv: - d["fgcolor"] = self.bgcolor - d["bgcolor"] = self.cur_c - else: - d["fgcolor"] = self.cur_c - elif mday in sundays: - d["fgcolor"] = self.sun_c - else: - d["fgcolor"] = self.fgcolor - d["text"] = str(mday) - self.grid[idx] = d + def days(self, month_length): # Produce content for every cell + for n in range(self.ncells + 1): + yield str(n + 1) if n < month_length else "" - today = DateCal() + def show(self): + grid = self.grid cur = self.date # Currency self.lbl.value(f"{DateCal.months[cur.month - 1]} {cur.year}") - d = {} # Args for Label.value - wday = 0 - wday_1 = cur.wday_n(1) # Weekday of 1st of month - mday = 1 - seek = True - sundays = cur.mday_list(6) - for idx in range(7, self.grid.ncells): - if seek: # Find column for 1st of month - if wday < wday_1: - self.grid[idx] = "" - wday += 1 - else: - seek = False - if not seek: - if mday <= cur.month_length: - cell() - mday += 1 - else: - self.grid[idx] = "" - idx = 7 # Where another row would be needed, roll over to top few cells. - while mday <= cur.month_length: - cell() - idx += 1 - mday += 1 + values = self.days(cur.month_length) # Instantiate generator + idx_1 = 7 + cur.wday_n(1) # Index of 1st of month + grid[idx_1 : self.last_cell] = values + grid[7 : idx_1] = values + # Assign colors. Last to be applied has priority. + grid[1:6, 6] = {"fgcolor": self.sun_c} # Sunday color + idx_cur = idx_1 + cur.mday - 1 # Currency (overrides Sunday) + if self.cur_inv: + grid[idx_cur] = {"fgcolor": self.bgcolor, "bgcolor": self.cur_c} + else: + grid[idx_cur] = {"fgcolor": self.cur_c} + today = DateCal() + if cur.year == today.year and cur.month == today.month: # Today is in current month + idx = idx_1 + today.mday - 1 + if self.today_inv: + grid[idx] = {"fgcolor": self.bgcolor, "bgcolor": self.fgcolor} + else: + grid[idx] = {"fgcolor": self.today_c}