By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

a question about tkinter StringVars()

P: n/a
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?

Thanks,
Bill
Aug 24 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Wed, 24 Aug 2005 15:07:27 GMT, William Gill <no*****@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+-'])"
Aug 24 '05 #2

P: n/a


Eric Brunel wrote:
On Wed, 24 Aug 2005 15:07:27 GMT, William Gill <no*****@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.

I will have to do some experimenting.
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 was trying to keep my question simple.
In actuality, I have a widget I'll call dataForm that extends
tkSimpleDialog.Dialog. In it I have several entry and checkbox widgets,
and a call to a changed() method passed from the parent. When I'm done
editing dataForm, all the variables are processed in a save() method,
and dataForm is destroyed.

What happened when I used self.var = MyStringVar() and Entry(root,
textvariable=self.var).pack(), is that a subsequent call to create a
dataForm instance has residual data from the previous instance AND the
change callback caused an error. Changing to local variables seemed to
cure the problems.

I just tried changing back to the self.var approach, and it seems to
work fine??? I must have had some name conflicts in my earlier code.
I'll try to figure it out, and post if I do.

Bill
Aug 24 '05 #3

P: n/a


Eric Brunel wrote:
On Wed, 24 Aug 2005 15:07:27 GMT, William Gill <no*****@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
Aug 25 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.