Gabriel Genellina wrote:
En Wed, 04 Mar 2009 12:12:50 -0200, W. eWatson <notval...@sbcglobal.net>
escribió:
That's fine, but I think my problem boils down to one question. There
seem to be two ways to communicate with a dialog (I mean a collection
of widgets assembled in a window that requires the user enter various
parameters, integers, strings, yes/no button, etc.): 1. a callback and
2. control variables. Which one should be used?
The simplest way that probably works. The one you feel most comfortable
with. The one best fits your application. There is no single answer.
To be more explicit, look at this code from your source above
<http://effbot.org/tkinterbook/entry.htm>. (This is about the simplest
"dialog" one can have.) :
[...code...]
Note that above this example, the author mentions:
"You can also bind the entry widget to a StringVar instance, and set
or get the entry text via that variable:
Why have two ways of doing this?
You may have some related widgets, and want to keep them syncronized. By
example, a slider 0-100 and a text entry for some percentage. Moving the
slider changes the number displayed, and editing the number moves the
slider accordingly. There are other ways of doing the same: you may
react to some events in one widget, and alter the other accordingly. But:
- that requires that you know *which* events actually modify the value
- increases coupling between all your widgets (consider what happens
when you want to add an image showing the effect of moving the slider)
So, in some cases, it is more convenient to use bound variables. You may
consider this as a micro-application of the Model-View-Controller pattern.
That said, I seldom use bound variables (but I seldom write GUIs using
Tkinter either).
Thanks, unfortunately I have no choice. I'm modifying a program that uses
Tkinter, and so not to take on extra work, I need to stick with Tkinter. The
author, for some reason, found it useful to use control variables.
In trying to add a configuration file to the code, the control variables
make it difficult. I have already coded the config file reading, writing,
and setting of variables (setattr came in very handy to get the fixed code
out of the way). However, to set them in the main dialog, is not easy,
because it uses code like self.colorVar = IntVar() and
dialog.colorVar.get(). To get rid of this written (hard) code, I have to
manufacture it somehow by forming it from the names found in the config
file, e.g., color=1. I'm aware of eval and exec, but wary of their use. It
seems like the solution to all this is not to use control variables, if
possible. Presently, I have no idea whether it is absolutely needed for the
program.
From what I can see on the web, and the few texts I have had access to, no
one makes a clear distinction between the two choices by way of words or
examples. This comes close:
From Tkinter reference: a GUI for Python
One special quality of a control variable is that it can be shared by a
number of different widgets, and the control variable can remember all the
widgets that are currently sharing it. This means, in particular, that if
your program stores a value v into a control variable c with its c.set(v)
method, any widgets that are linked to that control variable are
automatically updated on the screen.
I'm just getting into Tkinter, so that doesn't quite do it for me.
I originally mentioned, " ... it seems as though someone should have had a
similar problem in the past." What I meant was implementing a config file
arrangement with Tkinter. I was suggesting this seems nearly, if not,
impossible with control variables, so there must be another way. I think the
other way is described in the example I provided.
Well, I think it's now time to go back to the code, and see what I can do to
get rid of the control variables.
--
W. eWatson
(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet
Web Page: <www.speckledwithstars.net/>
--
http://mail.python.org/mailman/listinfo/python-list