Label widget: dynamic justify option.

pull/16/head
Peter Hinch 2022-04-09 11:21:04 +01:00
rodzic 9dd7368244
commit a248c4277b
2 zmienionych plików z 8 dodań i 4 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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