def __init__(self, master, text, width, foreground="black", truetype_font=None, font_path=None, family=None, size=None, **kwargs):
if truetype_font is None:
if font_path is None:
raise ValueError("Font path can't be None")
# Initialize font
truetype_font = ImageFont.truetype(font_path, size)
lines = textwrap.wrap(text, width=width)
width = 0
height = 0
line_heights = []
for line in lines:
line_width, line_height = truetype_font.getsize(line)
line_heights.append(line_height)
width = max(width, line_width)
height += line_height
image = Image.new("RGBA", (width, height), color=(0,0,0,0))
draw = ImageDraw.Draw(image)
y_text = 0
for i, line in enumerate(lines):
draw.text((0, y_text), line, font=truetype_font, fill=foreground)
y_text += line_heights[i]
self._photoimage = ImageTk.PhotoImage(image)
Label.__init__(self, master, image=self._photoimage, **kwargs)
评论列表
文章目录