kopia lustrzana https://github.com/peterhinch/micropython-micro-gui
Label widget: add justify option.
rodzic
d4fdc2e7f3
commit
9dd7368244
|
@ -995,6 +995,9 @@ Constructor args:
|
||||||
8. `bdcolor=False` Color of border. If `False` no border will be drawn. If
|
8. `bdcolor=False` Color of border. If `False` no border will be drawn. If
|
||||||
`None` the `fgcolor` will be used, otherwise a color may be passed. If a color
|
`None` the `fgcolor` will be used, otherwise a color may be passed. If a color
|
||||||
is available, a border line will be drawn around the control.
|
is available, a border line will be drawn around the control.
|
||||||
|
9. `justify=Label.LEFT` Options are `Label.RIGHT` and `Label.CENTRE` (note
|
||||||
|
British spelling). Justification can only occur if there is sufficient space
|
||||||
|
in the `Label` i.e. where an integer is supplied for the `text` arg.
|
||||||
|
|
||||||
The constructor displays the string at the required location.
|
The constructor displays the string at the required location.
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# aclock.py micro-gui analog clock demo.
|
# aclock.py micro-gui analog clock demo.
|
||||||
|
|
||||||
# Released under the MIT License (MIT). See LICENSE.
|
# Released under the MIT License (MIT). See LICENSE.
|
||||||
# Copyright (c) 2021 Peter Hinch
|
# Copyright (c) 2021-2022 Peter Hinch
|
||||||
|
|
||||||
# Initialise hardware and framebuf before importing modules.
|
# Initialise hardware and framebuf before importing modules.
|
||||||
import hardware_setup # Create a display instance
|
import hardware_setup # Create a display instance
|
||||||
|
@ -65,6 +65,7 @@ class BaseScreen(Screen):
|
||||||
labels = {'bdcolor' : RED,
|
labels = {'bdcolor' : RED,
|
||||||
'fgcolor' : WHITE,
|
'fgcolor' : WHITE,
|
||||||
'bgcolor' : DARKGREEN,
|
'bgcolor' : DARKGREEN,
|
||||||
|
'justify' : Label.CENTRE,
|
||||||
}
|
}
|
||||||
|
|
||||||
wri = CWriter(ssd, font, GREEN, BLACK) # verbose = True
|
wri = CWriter(ssd, font, GREEN, BLACK) # verbose = True
|
||||||
|
|
|
@ -8,8 +8,12 @@ from gui.core.colors import *
|
||||||
|
|
||||||
# text: str display string int save width
|
# text: str display string int save width
|
||||||
class Label(Widget):
|
class Label(Widget):
|
||||||
def __init__(self, writer, row, col, text, invert=False, fgcolor=None, bgcolor=BLACK, bdcolor=False):
|
LEFT = 0
|
||||||
|
CENTRE = 1
|
||||||
|
RIGHT = 2
|
||||||
|
def __init__(self, writer, row, col, text, invert=False, fgcolor=None, bgcolor=BLACK, bdcolor=False, justify=0):
|
||||||
self.writer = writer
|
self.writer = writer
|
||||||
|
self.justify = justify
|
||||||
# Determine width of object
|
# Determine width of object
|
||||||
if isinstance(text, int):
|
if isinstance(text, int):
|
||||||
width = text
|
width = text
|
||||||
|
@ -19,11 +23,14 @@ class Label(Widget):
|
||||||
height = writer.height
|
height = writer.height
|
||||||
self.invert = invert
|
self.invert = invert
|
||||||
super().__init__(writer, row, col, height, width, fgcolor, bgcolor, bdcolor)
|
super().__init__(writer, row, col, height, width, fgcolor, bgcolor, bdcolor)
|
||||||
|
self.tcol = col
|
||||||
if text is not None:
|
if text is not None:
|
||||||
self.value(text, invert)
|
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):
|
||||||
if self.writer.stringlen(text) > self.width: # Clip
|
sl = self.writer.stringlen(text)
|
||||||
|
self.tcol = self.col # Default is left justify
|
||||||
|
if sl > self.width: # Clip
|
||||||
font = self.writer.font
|
font = self.writer.font
|
||||||
pos = 0
|
pos = 0
|
||||||
n = 0
|
n = 0
|
||||||
|
@ -33,6 +40,11 @@ class Label(Widget):
|
||||||
break
|
break
|
||||||
n += 1
|
n += 1
|
||||||
text = text[: n]
|
text = text[: n]
|
||||||
|
elif self.justify == 1:
|
||||||
|
self.tcol = self.col + (self.width - sl) // 2
|
||||||
|
elif self.justify == 2:
|
||||||
|
self.tcol = self.col + self.width - sl
|
||||||
|
|
||||||
txt = super().value(text) # Sets .draw ensuring refresh
|
txt = super().value(text) # Sets .draw ensuring refresh
|
||||||
# Redraw even if no text supplied: colors may have changed.
|
# Redraw even if no text supplied: colors may have changed.
|
||||||
self.invert = invert
|
self.invert = invert
|
||||||
|
@ -46,4 +58,4 @@ class Label(Widget):
|
||||||
def show(self):
|
def show(self):
|
||||||
if super().show(): # Draw or erase border
|
if super().show(): # Draw or erase border
|
||||||
if isinstance(txt := super().value(), str):
|
if isinstance(txt := super().value(), str):
|
||||||
display.print_left(self.writer, self.col, self.row, txt, self.fgcolor, self.bgcolor, self.invert)
|
display.print_left(self.writer, self.tcol, self.row, txt, self.fgcolor, self.bgcolor, self.invert)
|
||||||
|
|
Ładowanie…
Reference in New Issue