a question about tkinter StringVars()

Eric Brunel eric_brunel at despammed.com
Wed Aug 24 11:42:23 EDT 2005


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?
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"



More information about the Python-list mailing list