How it works...

The FAMILIES tuple contains the three font families that Tk guarantees to support on all platforms: Times (Times New Roman), Courier, and Helvetica. They can be switched with the OptionMenu widget, which is connected to the self.family variable.

A similar approach is followed to set the font size with Spinbox. Both variables trigger the method that changes the font label:

def set_font(self, *args):
family = self.family.get()
size = self.size.get()
self.label.config(font=(family, size))

The tuple passed to the font option can also define one or more of the following font styles: boldromanitalicunderline, and strikethrough:

widget1.config(font=("Times", "20", "bold"))
widget2.config(font=("Helvetica", "16", "italic underline"))

You can retrieve the complete list of available font families for your platform with the families() method from the tkinter.font module. Since you need to instantiate the root window first, you can use the following script:

import tkinter as tk
from tkinter import font

root = tk.Tk()
print(font.families())

Tkinter will not throw any error if you use a font family that is not included in the list of available families, but will try to match a similar font.

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

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