[Tutor] Please Critque Tkinter Class for Newbie

Sheila King <sheila@thinkspot.net>, tutor@python.org Sheila King <sheila@thinkspot.net>, tutor@python.org
Mon, 09 Jul 2001 19:28:04 -0700


OK, I'm learning Tkinter, and have written a small Tkinter class, and would
appreciate the useful criticism of those more learned than myself. Anything
about style, efficiency, idioms, etc... that you think would improve the
code would be most welcome. 

Thanks. Code follows signature.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/



from Tkinter import *

colorVals = [ '00', '33', '66', '99', 'CC', 'FF' ]

class Palette(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        for item in colorVals:
            for tuple in [('00', '33', '66'), ('99', 'CC', 'FF')]:
                PlaqueRow(self, item, tuple).pack()
        self.pack()
        self.colortext=StringVar()
        self.info = colorInfo(self, self.colortext)
        self.info.pack(side=LEFT)
        self.colortext.set("")
        self.bind_all('<Button-1>', self.displayColor)

    def displayColor(self, event):
        self.colortext.set(event.widget.color)
        self.info.colorDisplay.config(bg=event.widget.color)

class colorInfo(Frame):
    def __init__(self, parent=None, text=""):
        Frame.__init__(self, parent, height=25, width=200)
        Label(self, text="click on a color", font=("bold", 12)).pack()
        self.colorDisplay = Label(self, width=6, height = 1, bg='#FFFFFF')
        self.colorDisplay.pack(side=LEFT)
        colorValue = Entry(self, width=10, textvariable = text)
        colorValue.pack(side=LEFT)
        
class PlaqueRow(Frame):
    def __init__(self, parent=None, leadcolor='00',
                         colortuple = ('00', '33', '66')):
        Frame.__init__(self, parent)
        for val in colortuple:
            for choice in colorVals:       # global variable-list of all colors
                block = Plaque(self, '#'+leadcolor+val+choice)
                block.config(bd='1')       # set border of button to one
                block.pack(side=LEFT)    

class Plaque(Button):
    def __init__(self, parent, color="#000000"):
        Button.__init__(self, parent, bg=color, activebackground=color,
                        height=1, width=2)
        self.color=color

if __name__ == '__main__':
    root = Tk()
    root.protocol("WM_DELETE_WINDOW", root.quit)
    palette = Palette(root)
    palette.pack()
    root.mainloop()