Configuration Files and Tkinter--Possible?

Gabriel Genellina gagsl-py2 at
Wed Mar 4 17:50:42 CET 2009

En Wed, 04 Mar 2009 13:50:32 -0200, W. eWatson <notvalid2 at>  
> Gabriel Genellina wrote:
>> En Wed, 04 Mar 2009 12:12:50 -0200, W. eWatson  
>> <notvalid2 at> 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  
>>> <>. (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.

You don't have to get rid of those bound variables. Just use them. Suppose  
your config object has an attribute "color" and the dialog has "colorVar"  
as you describe above. Then, in the setvalues method I menctioned in an  
earlier post, you can do:

	def setvalues(self, config):
	    # same for other vars

and make sure that, any way you handle the "Ok" button, the config obj is  

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

Maybe it helps to think of the control variable as a "model", and the  
associated widgets as their "views" -- a micro Model-View-Controller.

Gabriel Genellina

More information about the Python-list mailing list