Finer control over font

In addition to the aforementioned method of handling fonts, Tkinter provides a separate Font class implementation. The source code for this class is located in the same folder as the source code for Tkinter.

On my Linux machine, the source code is located in /usr/local/lib/python3.6/tkinter/font.py. On Windows (with a default Python 3.6 install) the location is C:Program Files (x86)Python36-32Lib kinterfont.py.

To use this module, you need to import fonts into your namespace, as follows (see the 10.08_font_demo.py code):

from tkinter import Tk, Label, Pack
from tkinter import font
root = Tk()
label = Label(root, text="Humpty Dumpty was pushed")
label.pack()
current_font = font.Font(font=label['font'])
print ('Actual :', str(current_font.actual()))
print ('Family : ', current_font.cget("family"))
print ('Weight : ', current_font.cget("weight"))
print ('Text width of Dumpty : {}'.format(current_font.measure("Dumpty")))
print ('Metrics:', str(current_font.metrics()))
current_font.config(size=14)
label.config(font=current_font)
print ('New Actual :', str(current_font.actual()))
root.mainloop()

The console output of this program on my terminal is as follows:

Actual: {'slant': 'roman', 'underline': 0, 'family': 'DejaVu Sans', 'weight': 'normal', 'size': -12, 'overstrike': 0}
Family: DejaVu Sans
Weight: normal
Text width of Dumpty: 49
Metrics: {'fixed': 0, 'descent': 3, 'ascent': 12, 'linespace':15}
New actual: {'slant': 'roman', 'underline': 0, 'family': 'DejaVu Sans', 'weight': 'normal', 'size': 14, 'overstrike': 0}

As you can see, the font module provides much better fine-grained control over various aspects of fonts that are otherwise inaccessible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.151.107