Skip to content

Commit dc3dcd2

Browse files
authored
Merge pull request #81 from kmatch98/anchor_fix
Updated label.py to correct position of initial blank text
2 parents ab0158d + b66efa1 commit dc3dcd2

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

adafruit_display_text/label.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ def __init__(
9595
self.width = max_glyphs
9696
self._font = font
9797
self._text = None
98-
if anchor_point is None:
99-
self._anchor_point = (0, 0)
100-
else:
101-
self._anchor_point = anchor_point
98+
self._anchor_point = anchor_point
10299
self.x = x
103100
self.y = y
104101

@@ -126,7 +123,7 @@ def __init__(
126123

127124
if text is not None:
128125
self._update_text(str(text))
129-
if anchored_position is not None:
126+
if (anchored_position is not None) and (anchor_point is not None):
130127
self.anchored_position = anchored_position
131128

132129
def _create_background_box(self, lines, y_offset):
@@ -374,14 +371,19 @@ def anchor_point(self):
374371

375372
@anchor_point.setter
376373
def anchor_point(self, new_anchor_point):
377-
current_anchored_position = self.anchored_position
378-
self._anchor_point = new_anchor_point
379-
self.anchored_position = current_anchored_position
374+
if self._anchor_point is not None:
375+
current_anchored_position = self.anchored_position
376+
self._anchor_point = new_anchor_point
377+
self.anchored_position = current_anchored_position
378+
else:
379+
self._anchor_point = new_anchor_point
380380

381381
@property
382382
def anchored_position(self):
383383
"""Position relative to the anchor_point. Tuple containing x,y
384384
pixel coordinates."""
385+
if self._anchor_point is None:
386+
return None
385387
return (
386388
int(self.x + (self._anchor_point[0] * self._boundingbox[2] * self._scale)),
387389
int(
@@ -393,6 +395,8 @@ def anchored_position(self):
393395

394396
@anchored_position.setter
395397
def anchored_position(self, new_position):
398+
if (self._anchor_point is None) or (new_position is None):
399+
return # Note: anchor_point must be set before setting anchored_position
396400
new_x = int(
397401
new_position[0]
398402
- self._anchor_point[0] * (self._boundingbox[2] * self._scale)

0 commit comments

Comments
 (0)