Tkinter Text family Parameter
family is used to set the glyph of the Text text area. The following example will illustrate the effect of this parameter on the glyph of the text area.
Example#1
Create a Text text area, and then create an OptionMenu object at the top, in this object is established within the Arial, Times, Courier three glyphs, of which, Arial is the default glyph, the user can enter text in the Text text area, and then select the glyph, you can see that the text entered will have different changes due to the selected glyph.
from tkinter import *
from tkinter.font import Font
def familyChanged(event): # font family changed
f=Font(family=familyVar.get()) # Get a new fong family
text.configure(font=f) # Update font family of text
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
# Create font family OptionMenu
familyVar = StringVar()
familyFamily = ("Arial","Times","Courier")
familyVar.set(familyFamily[0])
family = OptionMenu(root,familyVar,*familyFamily,command=familyChanged)
family.pack(pady=2)
# Create Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.focus_set()
root.mainloop()
Output:
Lines 13 to 18 of this program, regarding the creation of the OptionMenu object, can be found in Tkinter OptionMenu Tutorial;
Lines 21 to 23, about the creation of the Text text area can be referred to the description of the previous sections. The most important for the reader is line 6, you can get the selected font family, and then set in line 7 to let the Text text area use this font.
Example#2
The OptionMenu used in the above program example is a widget using tkinter, if you use tkinter.ttk you will see a different look
from tkinter import *
from tkinter.ttk import *
from tkinter.font import Font
def familyChanged(event): # font family changed
f=Font(family=familyVar.get()) # Get a new fong family
text.configure(font=f) # Update font family of text
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
# Create font family OptionMenu
familyVar = StringVar()
familyFamily = ("Arial","Times","Courier")
familyVar.set(familyFamily[0])
family = OptionMenu(root,familyVar,*familyFamily,command=familyChanged)
family.pack(pady=2)
# Create Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.focus_set()
root.mainloop()
Output: