32 lines
847 B
Python
32 lines
847 B
Python
from PIL import ImageDraw
|
|
|
|
from .base import View
|
|
|
|
|
|
class TextView(View):
|
|
def __init__(self, parent, size, center, text="", font_size=32, **kwargs):
|
|
self.text = text
|
|
self.font_size = font_size
|
|
super().__init__(parent, size, center, **kwargs)
|
|
|
|
def set_text(self, text):
|
|
self.text = text
|
|
|
|
def update_weight(self, weight):
|
|
im = self.bkg_im.copy()
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
if self.text:
|
|
# Estimate text dimensions for centering
|
|
char_w = self.font_size * 0.6
|
|
char_h = self.font_size
|
|
text_w = len(self.text) * char_w
|
|
text_h = char_h
|
|
|
|
x = self.center[0] - text_w / 2
|
|
y = self.center[1] - text_h / 2
|
|
|
|
draw.text((x, y), self.text, fill="black", font_size=self.font_size)
|
|
|
|
return im
|