tk_change_font.py

# =========================================================
# change font 
# =========================================================
# a button cycles through a list of fonts
# =========================================================

# ---------------------------------------------------------
# --- import
# ---------------------------------------------------------

import sys

if sys.version_info.major == 3:
    from tkinter import *
    from tkinter.font import *
else:
    from Tkinter import *
    from tkFont import *

# ---------------------------------------------------------
# --- global variables
# ---------------------------------------------------------

buttonFont = ('arial', '20', 'bold')

fontIndex = 0

fonts = [('Arial', '10', 'bold'), ('Arial', '15', 'bold'),
        ('courier', '12', 'roman'), ('Times', '12', 'roman'),
        ('Helvetica', '12', 'roman'),('Helvetica', '12', 'bold'),
        ('Helvetica', '12', 'italic'), ('Times', '12', 'bold italic')]

labelText = \
        'abcdefghijklmnopqrstuvwxyxABCDEFGHIJKLMNOPQRSTUVWXYZ'

# ---------------------------------------------------------
# functions
# ---------------------------------------------------------

def chageFont():
    global fontIndex
    fontIndex += 1
    if fontIndex >= len(fonts):
        fontIndex = 0
    l1.configure(font=fonts[fontIndex])
    l2.configure(font=fonts[fontIndex],
            text=fontDescription(fonts[fontIndex]))

def fontDescription(font):
    return '{}, {}, {}'.format(font[0],font[1],font[2])

def quit():
    sys.exit()

# ---------------------------------------------------------
# main
# ---------------------------------------------------------

if __name__ == '__main__':

    # -- Tk root window

    root = Tk()
    root.title('Change Font')

    # -- a lable to chage fonts

    l1 = Label(root, font=fonts[fontIndex], padx=4, pady=4,
            text=labelText)
    l1.pack()

    l2 = Label(root, font=fonts[fontIndex], padx=4, pady=4,
            text=fontDescription(fonts[fontIndex]))
    l2.pack()

    # --- buttons

    fb = Frame(root)

    b1 = Button(fb, text='Change Font', font=buttonFont,
            command=chageFont)

    b2 = Button(fb, text='Quit', font=buttonFont,
            command=quit)

    b1.grid(row=0, column=0)
    b2.grid(row=0, column=1)

    fb.pack()

    root.mainloop()