One Windows the best solution is to use the trick explained here:
http://stackoverflow.com/a/30631309
Another possibility is to use PIL. creating an image with the text and a specific font.
I provide 2 classes: CustomFont_Label and CustomFont_Message.
CustomFont_Message displays multilines but requires the width parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | # Author: Miguel Martinez Lopez
from PIL import Image, ImageFont, ImageDraw, ImageTk
import textwrap
try:
from Tkinter import Label
except ImportError:
from tkinter import Label
def truetype_font(font_path, size):
return ImageFont.truetype(font_path, size)
class CustomFont_Label(Label):
def __init__(self, master, text, 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)
width, height = truetype_font.getsize(text)
image = Image.new("RGBA", (width, height), color=(0,0,0,0))
draw = ImageDraw.Draw(image)
draw.text((0, 0), text, font=truetype_font, fill=foreground)
self._photoimage = ImageTk.PhotoImage(image)
Label.__init__(self, master, image=self._photoimage, **kwargs)
class CustomFont_Message(Label):
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)
if __name__ == "__main__":
try:
from Tkinter import Tk
except ImportError:
from tkinter import Tk
root = Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))
lorem_ipsum ="""Lorem ipsum dolor sit amet,
consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore
magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation
ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril
delenit augue duis dolore te feugait nulla facilisi."""
# Use your font here: font_path
CustomFont_Label(root, text="This is a text", font_path="OpenSans-Regular.ttf", size=22).pack()
CustomFont_Message(root, text=lorem_ipsum, width=40, font_path="OpenSans-Regular.ttf", size=22).pack(pady=(30,0))
root.mainloop()
|