a question about tkinter StringVars()

William Gill noreply at gcgroup.net
Thu Aug 25 11:46:53 EDT 2005



Eric Brunel wrote:
> On Wed, 24 Aug 2005 15:07:27 GMT, William Gill <noreply at gcgroup.net> wrote:
> 
>> Working with tkinter, I have a createWidgets() method in a class.
>> Within createWidgets() I create several StringVars() and
>> assign them to the textvariable option of several widgets.
>> Effectively my code structure is:
>>
>> def createWidgets(self):
>>      ...
>>      var = StringVar()
>>      Entry(master,textvariable=var)
>>      ...
>>      ...
>>
>> Though 'var' would normally go out of scope when createWidgets
>> completes, since the Entry and its reference do not go out of scope,
>> only the name 'var' goes out of scope, not the StringVar object, Right?
> 
> 
> Well, apparently not:
> 
> ------------------------------------------------
> from Tkinter import *
> 
> class MyStringVar(StringVar):
>   def __del__(self):
>     print "I'm dying!"
> 
> root = Tk()
> 
> def cw():
>   var = MyStringVar()
>   Entry(root, textvariable=var).pack()
> 
> cw()
> 
> root.mainloop()
> ------------------------------------------------
> 
> Running this script actually prints "I'm dying!", so there is obviously 
> no reference from the Entry widget to the variable object. The reference 
> is actually kept at tcl level between an entry and the *tcl* variable, 
> which knows nothing about the *Python* variable.
> 
> BTW, the whole purpose of StringVar's is to be kept so that the text for 
> the entry can be retrieved or modified by another part of the program. 
> So what can be the purpose of creating variables in a function or method 
> and not keeping them anywhere else than a local variable?

I posted that changing back to a non-local variable works now, and that 
my problem was probably name conflict.  I haven't been able to verify 
that, but I have to assume that was the original problem.

My band-aid may have 'worked' because tcl maintained the control 
variable and callback even though the Python variable was gone.

As far as "... the purpose of creating variables ... and not keeping 
them anywhere else...".  I actually was keeping them in a non-local 
list.  I was creating a local variable, appending it to the list, then 
reusing the local name for the next new control variable:
    ...
       var= IntVar()
       self.variables.append(var)
    ...
This was 'copied' from a snippet I was using as a template.

I now use:
    ...
       self.variables.append(IntVar())
    ...

Please let me know if I'm on thin ice here.

Bill



More information about the Python-list mailing list