473,585 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2015
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 "textvariab le") 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...@python ware.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 "textvariab le") 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 "textvariab le") 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 "textvariab le" option, not the "text" option.
Tk allows you to abbreviate option names.

and since you've set the "textvariab le" 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...@python ware.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 "textvariab le") 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 "textvariab le" option, not the "text" option.
Tk allows you to abbreviate option names.

and since you've set the "textvariab le" 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 "textvariab le" 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...@python ware.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 "textvariab le" 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
3834
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 less - during my own installation of Python 2.3 on Fedora Core 1 Linux for a friend of mine). Anyway, HTH, L.
10
3678
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. Andrew dalke@dalkescientific.com
6
2441
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 to run the most simplistic of python programs using Tkinter, I get an error message stating that Python can't find any tkinter module. Any ideas...
1
2224
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 2003, 00:49:11) on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import Tkinter Traceback (most recent call...
44
3672
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 comparison among them on easy coding, pythonish design, beautiful and generous looking, powerful development toolkit, and sufficient documentation,...
4
2099
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 below, then start the program with Application(), then click the Quit button, my terminal never prints anything again (such as commands I type).
13
3345
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 with: Each module to be removed needs to have a justification as to why it should no longer be distributed with Python. then goes on with,
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...
2
2157
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 Active State Python version 2.5 and found that there is not Tkinter and Tk module in it. To use Tkinter do I actually require Tk installed on my...
0
7908
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
7836
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
8336
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
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
6606
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
5710
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
5389
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...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.