Fonts

Let's start by changing the size, weight, slant, and family of the font used to display text. To specify the size, we provide the height as an integer in points. We can set the weight to either bold or normal and the slant to either italic (slanted) or roman (not slanted).

The font families we can use depend on what system the program is running on. common families include Times, courier, and Verdana, but dozens of others are usually available. One note of caution, though: if you choose an unusual font, people running your program on other computers might not have it, so your GUI might appear different than you'd like for them.

The following sets the font of a button to be 14-point, bold, italic, and Courier. The result is shown in Figure 14.9, on the following page.

Download gui/font.py

from Tkinter import * window = Tk()

button = Button(window, text="Hello", font=("Courier", 14, "bold italic"))

button.pack()

window.mainloop()

H O Otk

Eello

H O Otk

Eello

Figure 14.9: A button with customized text font

Was this article helpful?

0 0

Post a comment