# Tkinter Widget Demo
# Allen Downey
#
# This program demonstrates how to use the Gui module
# to create and operate on Tkinter widgets.
#
# The documentation for the widgets is at
# http://www.pythonware.com/library/tkinter/introduction/
#

from Gui import *

g = Gui(debug=True)

# FRAME 1

g.fr(LEFT)

la1 = g.la(TOP, text='This is a label.')

en = g.en(TOP, fill=NONE)
en.insert(END, 'This is an entry widget.')

la2 = g.la(TOP, text='')

def press_me():
    text = en.get()
    la2.configure(text=text)

bu = g.bu(TOP, text='Press me', command=press_me)

g.endfr()


# FRAME 2

g.fr(LEFT)

ca = g.ca(width=200, height=200)

item1 = ca.circle(0, 0, 70, 'red')
item2 = ca.rectangle([[0, 0], [60, 60]], 'blue')
item3 = ca.text([0, 0], 'This is a canvas.', 'white')

mb = g.mb(TOP, text='Choose a color')

def set_color(color):
    ca.itemconfig(item2, fill=color)

for color in ['red', 'green', 'blue']:
    g.mi(mb, color, command=Callable(set_color, color))
    
g.endfr()


# FRAME 3

g.fr(LEFT)

g.fr(TOP)
la = g.la(TOP, text='List of colors:')
lb = g.lb(LEFT)
sb = g.sb(RIGHT, fill=Y)
g.endfr()

def apply_color():
    t = lb.curselection()
    try:
        index = int(t[0])
        color = lb.get(index)
        ca.itemconfig(item1, fill=color)
    except:
        pass

bu = g.bu(BOTTOM, text='Apply color', command=apply_color)
g.endfr()

# fill the listbox with color names
fp = open('/usr/X11R6/lib/X11/rgb.txt')
fp.readline()

for line in fp:
    t = line.split('\t')
    name = t[2].strip()
    lb.insert(END, name)

# tell the listbox and the scrollbar about each other
lb.configure(yscrollcommand=sb.set)
sb.configure(command=lb.yview)


# FRAME 4

g.fr(LEFT)

te = g.te(TOP, fill=X, height=5, width=40)
te.insert(END, "This is a Text widget.\n")
te.insert(END, "It's like a little text editor.\n")
te.insert(END, "It had more than one line, unlike an Entry widget.\n")

st = g.st(TOP)
st.text.configure(height=5, width=40)

st.text.insert(END, "This is a Scrollable Text widget.\n")
st.text.insert(END, "It is defined in Gui.py\n")

for i in range(100):
    st.text.insert(END, "All work and no play.\n")

g.endfr()


# FRAME 5

g.fr(LEFT)

def print_var(obj):
    print obj.var.get()


g.la(TOP, text='Font:')
fontsize = IntVar()

for size in [10, 12, 14, 16, 18]:
    rb = g.rb(TOP, text=str(size), variable=fontsize, value=size)
    rb.configure(command=Callable(print_var, rb))

b1 = g.cb(TOP, text='Bold')
b1.configure(command=Callable(print_var, b1))

b2 = g.cb(TOP, text='Italic')
b2.configure(command=Callable(print_var, b2))

g.endfr()

# FRAME 5

# start a grid with three columns
g.gr(3, rweights=[1,1,1], side=LEFT)

for i in range(1, 10):
    g.bu(text=str(i), sticky=NS)

g.endgr()

g.mainloop()
