[Tutor] Please Critque Tkinter Class for Newbie

Michael P. Reilly arcege@speakeasy.net
Tue, 10 Jul 2001 08:09:46 -0400 (EDT)


notme@gte.net wrote
> 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. 

Just two comments.  A widget should not pack itself as your Palette does.
That should be the application's choice.  If there are some "adjustments"
you want, then override the method to modify the parameters.

The colorInfo widget gets passed a StringVar instance, but the
default is a (empty) string.  The system might get a little
confused: Tk (under Tkinter) uses the name of the variable,
the default string could be taken without an error, but lead to
some odd problems.  You probably want to test before using the
value and if not value, then possibly raise an exception or create
a new variable.

Other than that, it looks pretty good.
  -Arcege

> 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()
          XXXXXXXXXXX # allow application to 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)
<snipped code>

> 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)
          if text == "":
            raise ValueError("text must be StringVar instance")

>         colorValue = Entry(self, width=10, textvariable = text)
>         colorValue.pack(side=LEFT)
<snipped code>

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |