kopia lustrzana https://github.com/peterhinch/micropython-micro-gui
Simplfy calendar demo. Fix Label where text is None.
rodzic
28db6e7240
commit
d886989f7e
|
@ -28,6 +28,8 @@ class BaseScreen(Screen):
|
||||||
row = 2
|
row = 2
|
||||||
rows = 6
|
rows = 6
|
||||||
cols = 7
|
cols = 7
|
||||||
|
self.ncells = cols * (rows - 1) # Row 0 has day labels
|
||||||
|
self.last_cell = cols * rows
|
||||||
colwidth = 35
|
colwidth = 35
|
||||||
self.lbl = Label(wri, row, col, text = (colwidth + 4) * cols, justify=Label.CENTRE)
|
self.lbl = Label(wri, row, col, text = (colwidth + 4) * cols, justify=Label.CENTRE)
|
||||||
row = self.lbl.mrow
|
row = self.lbl.mrow
|
||||||
|
@ -72,49 +74,28 @@ class BaseScreen(Screen):
|
||||||
d.now()
|
d.now()
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def update(self):
|
def days(self, month_length): # Produce content for every cell
|
||||||
def cell():
|
for n in range(self.ncells + 1):
|
||||||
if cur.year == today.year and cur.month == today.month and mday == today.mday: # Today
|
yield str(n + 1) if n < month_length else ""
|
||||||
d["fgcolor"] = RED
|
|
||||||
elif mday == cur.mday: # Currency
|
|
||||||
d["fgcolor"] = YELLOW
|
|
||||||
elif mday in sundays:
|
|
||||||
d["fgcolor"] = BLUE
|
|
||||||
else:
|
|
||||||
d["fgcolor"] = GREEN
|
|
||||||
d["text"] = str(mday)
|
|
||||||
self.grid[idx] = d
|
|
||||||
|
|
||||||
today = DateCal()
|
def update(self):
|
||||||
|
grid = self.grid
|
||||||
cur = self.date # Currency
|
cur = self.date # Currency
|
||||||
self.lbl.value(f"{DateCal.months[cur.month - 1]} {cur.year}")
|
self.lbl.value(f"{DateCal.months[cur.month - 1]} {cur.year}")
|
||||||
d = {} # Args for Label.value
|
# Populate day number cells
|
||||||
wday = 0
|
values = self.days(cur.month_length) # Instantiate generator
|
||||||
wday_1 = cur.wday_n(1) # Weekday of 1st of month
|
idx_1 = 7 + cur.wday_n(1) # Index of 1st of month
|
||||||
mday = 1
|
grid[idx_1 : self.last_cell] = values # Populate from mday 1 to last cell
|
||||||
seek = True
|
grid[7 : idx_1] = values # Populate cells before 1st of month
|
||||||
sundays = cur.mday_list(6)
|
# Color cells of Sunday, today and currency. In collisions (e.g. today==Sun)
|
||||||
for idx in range(7, self.grid.ncells):
|
# last color applied is effective
|
||||||
if seek: # Find column for 1st of month
|
grid[1:6, 6] = {"fgcolor": BLUE} # Sunday color
|
||||||
if wday < wday_1:
|
grid[idx_1 + cur.mday - 1] = {"fgcolor": YELLOW} # Currency
|
||||||
self.grid[idx] = ""
|
today = DateCal()
|
||||||
wday += 1
|
if cur.year == today.year and cur.month == today.month: # Today is in current month
|
||||||
else:
|
grid[idx_1 + today.mday - 1] = {"fgcolor": RED}
|
||||||
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
|
|
||||||
self.lblnow.value(f"{today.day_str} {today.mday} {today.month_str} {today.year}")
|
self.lblnow.value(f"{today.day_str} {today.mday} {today.month_str} {today.year}")
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
print('Calendar demo.')
|
print('Calendar demo.')
|
||||||
Screen.change(BaseScreen) # A class is passed here, not an instance.
|
Screen.change(BaseScreen) # A class is passed here, not an instance.
|
||||||
|
|
|
@ -28,24 +28,25 @@ class Label(Widget):
|
||||||
self.value(text, invert)
|
self.value(text, invert)
|
||||||
|
|
||||||
def value(self, text=None, invert=False, fgcolor=None, bgcolor=None, bdcolor=None, justify=None):
|
def value(self, text=None, invert=False, fgcolor=None, bgcolor=None, bdcolor=None, justify=None):
|
||||||
sl = self.writer.stringlen(text)
|
if text is not None:
|
||||||
if justify is None:
|
sl = self.writer.stringlen(text)
|
||||||
justify = self.justify
|
if justify is None:
|
||||||
self.tcol = self.col # Default is left justify
|
justify = self.justify
|
||||||
if sl > self.width: # Clip
|
self.tcol = self.col # Default is left justify
|
||||||
font = self.writer.font
|
if sl > self.width: # Clip
|
||||||
pos = 0
|
font = self.writer.font
|
||||||
n = 0
|
pos = 0
|
||||||
for ch in text:
|
n = 0
|
||||||
pos += font.get_ch(ch)[2] # width of current char
|
for ch in text:
|
||||||
if pos > self.width:
|
pos += font.get_ch(ch)[2] # width of current char
|
||||||
break
|
if pos > self.width:
|
||||||
n += 1
|
break
|
||||||
text = text[: n]
|
n += 1
|
||||||
elif justify == 1: # Centre
|
text = text[: n]
|
||||||
self.tcol = self.col + (self.width - sl) // 2
|
elif justify == 1: # Centre
|
||||||
elif justify == 2: # Right
|
self.tcol = self.col + (self.width - sl) // 2
|
||||||
self.tcol = self.col + self.width - sl
|
elif justify == 2: # Right
|
||||||
|
self.tcol = self.col + self.width - sl
|
||||||
|
|
||||||
txt = super().value(text)
|
txt = super().value(text)
|
||||||
self.draw = True # Redraw unconditionally: colors may have changed.
|
self.draw = True # Redraw unconditionally: colors may have changed.
|
||||||
|
|
Ładowanie…
Reference in New Issue