473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Odd behavior in Python/Tkinter?

Lie
Inspect the following code:

--- start of code ---
import Tkinter as Tk
from Tkconstants import *

root = Tk.Tk()

e1 = Tk.Entry(root, text = 'Hello World')
e2 = Tk.Entry(root, text = 'Hello World')

e1.grid(row = 1, column = 1)
e2.grid(row = 2, column = 1)

e1.insert(END, 'Hello Python')

root.mainloop()

--- end of code ---

What do you expect the result should be?
a. Two textboxes: One contains 'Hello Python', the other 'Hello World'
b. Two textboxes: Both containing 'Hello World'
c. Two textboxes: Both containing 'Hello Python'
d. Two textboxes: Both empty
e. Don't know

Check your guess with your Python 2.5.1 (AFAIK, the latest version at
the moment of writing)
Dec 21 '07 #1
8 2004
Lie wrote:
Inspect the following code:

--- start of code ---
import Tkinter as Tk
from Tkconstants import *

root = Tk.Tk()

e1 = Tk.Entry(root, text = 'Hello World')
e2 = Tk.Entry(root, text = 'Hello World')
the "text" (or "textvariable") option to the Entry widget is the name of
the Tcl variable that should hold the result.

to get something that's a bit more usable from Python, use a StringVar
instance instead. alternatively, use the "get" method to fetch text
from the widget, and the "insert" method to add text to it.

also see:

http://effbot.org/tag/Tkinter.entry

</F>

Dec 21 '07 #2
On Dec 21, 12:30 pm, Lie <Lie.1...@gmail.comwrote:
Inspect the following code:

--- start of code ---
import Tkinter as Tk
from Tkconstants import *

root = Tk.Tk()

e1 = Tk.Entry(root, text = 'Hello World')
e2 = Tk.Entry(root, text = 'Hello World')

e1.grid(row = 1, column = 1)
e2.grid(row = 2, column = 1)

e1.insert(END, 'Hello Python')

root.mainloop()

--- end of code ---

What do you expect the result should be?
a. Two textboxes: One contains 'Hello Python', the other 'Hello World'
b. Two textboxes: Both containing 'Hello World'
c. Two textboxes: Both containing 'Hello Python'
d. Two textboxes: Both empty
e. Don't know

Check your guess with your Python 2.5.1 (AFAIK, the latest version at
the moment of writing)


Huh. I got C (using python 2.4.3 on Ubuntu Linux..)

That ain't right.
Dec 21 '07 #3
Lie
On Dec 22, 4:05*am, Fredrik Lundh <fred...@pythonware.comwrote:
Lie wrote:
Inspect the following code:
--- start of code ---
import Tkinter as Tk
from Tkconstants import *
root = Tk.Tk()
e1 = Tk.Entry(root, text = 'Hello World')
e2 = Tk.Entry(root, text = 'Hello World')

the "text" (or "textvariable") option to the Entry widget is the name of
the Tcl variable that should hold the result.

to get something that's a bit more usable from Python, use a StringVar
instance instead. *alternatively, use the "get" method to fetch text
from the widget, and the "insert" method to add text to it.

also see:

* * *http://effbot.org/tag/Tkinter.entry

</F>
I realized that 'text' isn't a normally valid arguments for Entry (and
I'm also aware about the insert, get, etc), but what makes me creep is
the fact that the Entry's value would mirror each other if they're set
to the same 'text' value (Actually, I'm interested on how they
implement the Tkinter's/Tcl/Tk Entry box that this kind of behavior
could ever possibly exist [I started Pythoning just two weeks ago, so
my knowledge on its internals is quite limited]).
Dec 21 '07 #4
Lie wrote:
>>Inspect the following code:

--- start of code ---
import Tkinter as Tk
from Tkconstants import *
root = Tk.Tk()
e1 = Tk.Entry(root, text = 'Hello World')
e2 = Tk.Entry(root, text = 'Hello World')

the "text" (or "textvariable") option to the Entry widget is the name of
the Tcl variable that should hold the result.

to get something that's a bit more usable from Python, use a StringVar
instance instead. alternatively, use the "get" method to fetch text
from the widget, and the "insert" method to add text to it.
I realized that 'text' isn't a normally valid arguments for Entry (and
I'm also aware about the insert, get, etc), but what makes me creep is
the fact that the Entry's value would mirror each other if they're set
to the same 'text' value
note that you've set the "textvariable" option, not the "text" option.
Tk allows you to abbreviate option names.

and since you've set the "textvariable" option for both widgets to the
same variable, you've asked them both to display the same variable.
Tkinter just does what you've asked it to.

this is no different from using a Tkinter variable to display the same
value in a number of radiobutton widgets.

</F>

Dec 22 '07 #5
Lie
On Dec 22, 1:42*pm, Fredrik Lundh <fred...@pythonware.comwrote:
Lie wrote:
>Inspect the following code:
>--- start of code ---
import Tkinter as Tk
from Tkconstants import *
root = Tk.Tk()
e1 = Tk.Entry(root, text = 'Hello World')
e2 = Tk.Entry(root, text = 'Hello World')
the "text" (or "textvariable") option to the Entry widget is the name of
the Tcl variable that should hold the result.
to get something that's a bit more usable from Python, use a StringVar
instance instead. *alternatively, use the "get" method to fetch text
from the widget, and the "insert" method to add text to it.
I realized that 'text' isn't a normally valid arguments for Entry (and
I'm also aware about the insert, get, etc), but what makes me creep is
the fact that the Entry's value would mirror each other if they're set
to the same 'text' value

note that you've set the "textvariable" option, not the "text" option.
Tk allows you to abbreviate option names.

and since you've set the "textvariable" option for both widgets to the
same variable, you've asked them both to display the same variable.
Tkinter just does what you've asked it to.

this is no different from using a Tkinter variable to display the same
value in a number of radiobutton widgets.

</F>
But an expression (e.g. string) is NOT a variable. It's fine if the
value mirrored when I set the textvariable conf to the same variable,
but in this case I'm setting them to the same expression (e.g. string).
Dec 22 '07 #6
Lie
But an expression (e.g. string) is NOT a variable. It's fine if the
value mirrored when I set the textvariable conf to the same variable,
but in this case I'm setting them to the same expression (e.g. string).
On the other hand, the oddness multiplied since the value replication
doesn't happen if I set the textvariable to a variable. So, why is it?
A checkbox/optionbox value replication works if they're set to a
variable (I don't know if they're set to an expression) but Entry's
value replication works only if they're set to expression not variable.
Dec 22 '07 #7
Lie wrote:
But an expression (e.g. string) is NOT a variable.
in this case, it is. I don't know if it's worth spending more time on
this, since you're not listening, but let's make one more attempt.

for the Entry widget, the "textvariable" argument, if given, identifies
an *internal* Tkinter variable (managed by the embedded Tcl inter-
preter, not Python). changes to this variable will be reflected in the
widget, and changes to the widget will be reflected in the variable.

the *usual* way to create such a variable is to use StringVar, and leave
it to Tkinter to come up with an internal variable name, but you can
refer to any Tcl variable; if it doesn't exist, it's created.

in your example, you told both widgets to use the same internal
variable. you can do the same thing with a StringVar:

var = Tk.StringVar()

e1 = Tk.Entry(root, textvariable = var)
e2 = Tk.Entry(root, textvariable = var)

doing this has the advantage that you can access the internal variable
via the StringVar object (held in the Python variable named "var"), but
at the Tkinter level, it's exactly the same thing. changes to the
variable will be reflected in both widgets, and changes to *either*
widget will be reflected in the variable, and therefore also in the
other widget.
On the other hand, the oddness multiplied since the value replication
doesn't happen if I set the textvariable to a variable.
sure does, if you use the same internal variable for both widgets.

</F>

Dec 22 '07 #8
Lie
On Dec 22, 7:35*pm, Fredrik Lundh <fred...@pythonware.comwrote:
Lie wrote:
But an expression (e.g. string) is NOT a variable.

in this case, it is. *I don't know if it's worth spending more time on
this, since you're not listening, but let's make one more attempt.
Sure I'm listening (well, actually I'm reading), but please spare me
since I can't understand what you meant in the previous posts (I'm
just starting Python, and yesterday was my first introduction to
Tkinter/Tk/Tcl). Everyone has their own newbie moments...
for the Entry widget, the "textvariable" argument, if given, identifies
an *internal* Tkinter variable (managed by the embedded Tcl inter-
preter, not Python). *changes to this variable will be reflected in the
widget, and changes to the widget will be reflected in the variable.
That clears things up. I never realized that we can even specify the
name for the Tcl's variable, setting its values is a behavior, but
setting its name is... some kind of incomplete encapsulation (well its
understandable, complete encapsulation is never a Pythonic thing and
proper ways to keep the encapsulation is available [through
StringVar]).
the *usual* way to create such a variable is to use StringVar, and leave
it to Tkinter to come up with an internal variable name, but you can
refer to any Tcl variable; if it doesn't exist, it's created.

in your example, you told both widgets to use the same internal
variable. *you can do the same thing with a StringVar:

* * *var = Tk.StringVar()

* * *e1 = Tk.Entry(root, textvariable = var)
* * *e2 = Tk.Entry(root, textvariable = var)

doing this has the advantage that you can access the internal variable
via the StringVar object (held in the Python variable named "var"), but
at the Tkinter level, it's exactly the same thing. *changes to the
variable will be reflected in both widgets, and changes to *either*
widget will be reflected in the variable, and therefore also in the
other widget.

*On the other hand, the oddness multiplied since the value replication
*doesn't happen if I set the textvariable to a variable.

sure does, if you use the same internal variable for both widgets.
After reading your post, I realized the reason why it doesn't
replicate was because I set the variable to an empty string, which
doesn't initialize the textvariable.
Dec 22 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
6
by: Peter Milliken | last post by:
Hi, I (think I have :-)) installed Python on my Pocket PC (obtained from http://fore.validus.com/~kashtan/). There were Tkinter binaries with it so I installed those as well. When I attempt...
1
by: John Chambers | last post by:
Sp my latest adventure is attempting to use python's Tkinter module on a few machines. On my PB (OSX 10.3.9), I got the following confusing results: /Users/jc: python Python 2.3 (#1, Sep 13...
44
by: jiang.haiyun | last post by:
Now i began to learn GUI programming. There are so many choices of GUI in the python world, wxPython, pyGTK, PyQT, Tkinter, .etc, it's difficult for a novice to decide, however. Can you draw a...
4
by: Chris | last post by:
Hi, I'm puzzled by some strange behavior when my Python/Tkinter application quits (on linux): the terminal from which I started Python is messed up. If start up python, then import the code...
13
by: Daniel Fetchinson | last post by:
Was looking at PEP 3108, http://www.python.org/dev/peps/pep-3108/ , Is it just me or others also think that it would be a major loss to remove tkinter from the python core? PEP 3108 starts off...
8
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...
2
by: Dudeja, Rajat | last post by:
Hi, So, now I've finally started using Eclipse and PyDev as an IDE for my GUI Application. I just wrote some sample programs as an hands on. Now I would like to take up Tkinter. I'm using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
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,...
0
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...

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.