473,584 Members | 2,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a question about tkinter StringVars()

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(s elf):
...
var = StringVar()
Entry(master,te xtvariable=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
3 2220
On Wed, 24 Aug 2005 15:07:27 GMT, William Gill <no*****@gcgrou p.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(s elf):
...
var = StringVar()
Entry(master,te xtvariable=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(Str ingVar):
def __del__(self):
print "I'm dying!"

root = Tk()

def cw():
var = MyStringVar()
Entry(root, textvariable=va r).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.zmz 5(17;8(%,5.Z65\ '*9--56l7+-'])"
Aug 24 '05 #2


Eric Brunel wrote:
On Wed, 24 Aug 2005 15:07:27 GMT, William Gill <no*****@gcgrou p.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(s elf):
...
var = StringVar()
Entry(master,te xtvariable=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(Str ingVar):
def __del__(self):
print "I'm dying!"

root = Tk()

def cw():
var = MyStringVar()
Entry(root, textvariable=va r).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=se lf.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


Eric Brunel wrote:
On Wed, 24 Aug 2005 15:07:27 GMT, William Gill <no*****@gcgrou p.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(s elf):
...
var = StringVar()
Entry(master,te xtvariable=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(Str ingVar):
def __del__(self):
print "I'm dying!"

root = Tk()

def cw():
var = MyStringVar()
Entry(root, textvariable=va r).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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7014
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter import * class App:
7
11892
by: SeeBelow | last post by:
Do many people think that wxPython should replace Tkinter? Is this likely to happen? I ask because I have just started learning Tkinter, and I wonder if I should abandon it in favor of wxPython. Mitchell Timin -- "Many are stubborn in pursuit of the path they have chosen, few in
1
2962
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the scroll-bars work as advertised. That's great. However, if you click the Left Mouse button, it calls code which expands the width of the canvas by 100...
0
3572
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust accordingly. However, what I really want to happen is that the area of the canvas that the scrollbars show (the Scrollregion) should expand as the...
1
3588
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that I am hoping someone knows what I am doing wrong: 1) Pressing the Settings.. Button multiple times, brings up many instances of the Settings...
4
5486
by: Kevin Walzer | last post by:
I'm trying to manage user preferences in a Tkinter application by initializing some values that can then be configured from a GUI. The values are set up as a dict, like so: self.prefs= { 'interface': '-en1', 'verbose': '-v', 'fontname': 'Courier', 'point': 12, }
4
1988
by: fabdeb | last post by:
Hi every one, I m a sysadmin who want to know how to use python. I dont know anything about oriented object programation, i only know bash and a little perl. I have some simple questions about python. the first: what is the differences between a function and a classe? In which case i should use a function ? In which case i should use a...
3
4189
by: seanacais | last post by:
I'm trying to build an unknown number of repeating gui elements dynamically so I need to store the variables in a list of dictionaries. I understand that Scale "variable" name needs to be a StringVar but I cannot figure out how to initialize the dictionary. I've tried the following code ps = PowerSupply() # Instantiate a Power Supply...
8
3272
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to my pc. But, it is not getting installed and is failing by throwing the below errors. Should i need to configure / install any specific files for...
0
7897
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7829
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8190
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7940
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6590
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5705
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5379
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1441
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.