From a248c4277bf3f0e30e2d2402aedf02e2d12804bd Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Sat, 9 Apr 2022 11:21:04 +0100 Subject: [PATCH] Label widget: dynamic justify option. --- README.md | 4 +++- gui/widgets/label.py | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fa0f249..754ab26 100644 --- a/README.md +++ b/README.md @@ -1009,7 +1009,9 @@ Method: * `bgcolor=None` Background color, as per foreground. * `bdcolor=None` Border color. As per above except that if `False` is passed, no border is displayed. This clears a previously drawn border. -Returns the current text string. +Returns the current text string. + * `justify=None` By default justify using the constructor default. Override + with `Label.LEFT`, `Label.RIGHT` or `Label.CENTRE`. If the `value` method is called with a text string too long for the `Label` the text will be clipped to fit the width. In this case `value()` will return the diff --git a/gui/widgets/label.py b/gui/widgets/label.py index 8141bf9..e32796e 100644 --- a/gui/widgets/label.py +++ b/gui/widgets/label.py @@ -27,8 +27,10 @@ class Label(Widget): if text is not None: self.value(text, invert) - def value(self, text=None, invert=False, fgcolor=None, bgcolor=None, bdcolor=None): + def value(self, text=None, invert=False, fgcolor=None, bgcolor=None, bdcolor=None, justify=None): sl = self.writer.stringlen(text) + if justify is None: + justify = self.justify self.tcol = self.col # Default is left justify if sl > self.width: # Clip font = self.writer.font @@ -40,9 +42,9 @@ class Label(Widget): break n += 1 text = text[: n] - elif self.justify == 1: + elif justify == 1: # Centre self.tcol = self.col + (self.width - sl) // 2 - elif self.justify == 2: + elif justify == 2: # Right self.tcol = self.col + self.width - sl txt = super().value(text) # Sets .draw ensuring refresh