Tkinter - Selecting Fonts in Python

This is example which shows how to select and change fonts for elements of a tkinter GUI.

import tkinter

#create a window
window = tkinter.Tk()

#Sets window title
window.title("Font Example")

#Create a Label
lbldoctype = tkinter.Label(window,
                           text="   Option Choice:    ",
                           fg="#FFFFFF",            #we define the colour of the text
                           bg="#FF0000",            #we define the colour of the background for the label
                           font=("Helvetica", 12)   #here we define the font and size of the text
                           )
lbldoctype.pack()

#create a button widget called btn
btn = tkinter.Button(window,
                     text="     Select     ",
                           fg="#00FF00",        # we define the colour of the text
                           bg="#FF00FF",        # we define the colour of the background for the label
                           font=("Times", 12)   # here we define the font and size of the text
                     
                     )
#pack the button into the window
btn.pack()

#Loops the tkinter window
window.mainloop()
This creates this wonderful looking window

Note that colours are defined in hex notation (three byte hexidecimal). It is also possible to select colours by name. The fonts depend on the system being utilised.







Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.
Tkinter - Learn the key features of the tkinter to allow you to create user interfaces for your python programs.


Must Read Articles

Tkinter - Combo Box - This is a short example showing how to use combo boxes in a GUI.
Tkinter - Radio Buttons - A short example of how to create a GUI with radio buttons.
Tkinter _ Canvas - Canvas allows for a range of shapes to be plotted, such as circles, squares, lines and labels.
Tkinter - Pack vs Grid vs Place - tkinter provides flexibility in how the layout the widgets. 
Pickle - The pickle library allows a program to save data in a binary file.
Python - List Comprehension - Create powerful list comprehension expressions.
Python - Generators - Learn about generator statements.
Python - Data types - The learn about the key datatypes in the python language.