tk_change_bg.py

# =========================================================
# change background color
# =========================================================
# a button cycles through a list of colors
# =========================================================

# ---------------------------------------------------------
# --- 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')

colorIndex = 0

colors = ['red','green','blue','light blue',
        'pink','orange']

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

def chageColor():
    global colorIndex
    colorIndex += 1
    if colorIndex >= len(colors):
        colorIndex = 0
    f.configure(bg=colors[colorIndex])

def quit():
    sys.exit()

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

if __name__ == '__main__':

    # -- Tk root window

    root = Tk()
    root.title('Change Background Color')

    # -- a frame to chage colors

    f = Frame(root, height=400, width=400, bg=colors[colorIndex])
    f.grid(row=0, column=0)

    # --- buttons

    fb = Frame(root)
    fb.grid(row=1,column=0)

    b1 = Button(fb, text='change Color', font=buttonFont,
            command=chageColor)

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

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

    fb.grid(row=1, column=0)

    root.mainloop()