[Tutor] Please Critque Tkinter Class for Newbie

Sheila King sheila@thinkspot.net
Wed, 11 Jul 2001 23:33:13 -0700


On Tue, 10 Jul 2001 08:09:46 -0400 (EDT), "Michael P. Reilly"
<arcege@dsl092-074-184.bos1.dsl.speakeasy.net>  wrote about Re: [Tutor]
Please Critque Tkinter Class for Newbie:

: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.

OK, I'm working on understanding the issue, here. I guess I didn't
really understand the distinction between the text and textvariable
being string vs. StringVar types, and it must've completely passed over
my head, that the option on the Entry widget is a textvariable (requires
a Tkinter variable class).

So, I'm experimenting with some other stuff, to see if I understand this
whole thing, and I write the following code:

-----------------------------------------------------------
from Tkinter import *

root = Tk()
ent = Entry(root, text = "Enter your name")
ent.pack()
root.mainloop()
-----------------------------------------------------------

And the weird thing, is that although the Entry widget doesn't have a
text option, this causes no error message. (It doesn't display the text
"Enter your name", either, but then I didn't expect that it would.) Most
of these widgets give me an error like "Such-and-such-widget doesn't
have a <blank> method/attribute". But the Entry widget isn't doing that
here.

Now if I change the line 
ent = Entry(root, text = "Enter your name")
to
ent = Entry(root, textvariable = "Enter your name")

I get the same behavior as before...no error message, but it doesn't
display my string, either.

Now, when I replaced it with this line:
ent = Entry(root, textvariable = StringVar("Enter your name"))
it raised an exception (which I expected).

I do see what needs to be done. An instance of a textvariable must be
created before an instance of the class colorInfo is created, and it
must be passed to the instance at invocation (which is what I was
doing). But the default empty string is the wrong type and doesn't work.
You suggested raising an exception and dealing with that. I suppose that
is one possibility. For now, I think I will just remove that default
parameter from my class. If the user doesn't pass a StringVar type,
though, the Hex codes for the colors will never appear in the Entry
Widget. Then, again, I don't expect the colorInfo class will ever be
used outside of my Palette class, so that situation isn't likely to
occur.

I guess there is no recommended way to pass default parameters to
Tkinter variables such as the StringVar type (and IntVar and DoubleVar)?


:Other than that, it looks pretty good.
:  -Arcege

Thanks!

--
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()
:          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>