tkinter questions: behavior of StringVar, etc

Francesco Bochicchio bockman at virgilio.it
Sun Mar 29 13:29:44 CEST 2009


Alan G Isaac ha scritto:
>> On Mar 28, 2:15 pm, Alan G Isaac <alan.is... at gmail.com> wrote:
>>> I'm a complete newbie to GUI. I have a couple questions about tkinter. 
> 
>>> 1. Where is the list of changes     in Python 3's tkinter?
> 
>>> 2. What exactly is the role of the root object,     traditionally 
>>> created as ``root=tk.Tk()``?
>>>     What is an example where one should create this
>>>     before creating a Frame instance (which will
>>>     otherwise implicitly create one as its master)?
> 
>>> 2'. Suppose I ``import tkinter as tk`` and     then try 
>>> ``s1=tk.StringVar()``.  This fails
>>>     because no "master" is set. Why does a
>>>     Variable need a master?
> 
>>> 3. Now suppose I set ``root = tk.TK()`` and     then try 
>>> ``s1=tk.StringVar()``.  This
>>>     works fine but now seems a bit magical:
>>>     how has the value of the "master" been
>>>     set?
> 
>>> 4. Another bit of magic:
>>>     Suppose I ``import tkinter as tk`` and
>>>     then try ``f1=tk.Frame()``.  This works
>>>     fine: apparently calling Frame also
>>>     leads to implicit creation of a "master".
>>>     Why is what is good for the gander (i.e.,
>>>     implicit master creation for a Frame) not
>>>     good for the goose (i.e., a Variable)?
>>>     (Here I assume that there has been an
>>>     answer to 2. above.)
> 
>>> 5. Reading around a bit,     it seems common to recommend setting
>>>     the values of Variables rather than initializing
>>>     them.  Why?  I cannot see the reason to avoid
>>>     ``s1=tk.StringVar(value="this works fine")``
>>>     and it looks like ``tk.StringVar(()`` is in any
>>>     case initialized (to an empty string).
> 
>>> 6. Why is str(s1) not its value?  More generally,     why does a 
>>> StringVar not behave more like a string?
> 
> 
> 
> On 3/28/2009 6:19 PM Mike Driscoll apparently wrote:
>> Try Google and the Python website. There is tons of info on the Python 
>> wiki:
>> http://wiki.python.org/moin/TkInter There are also some books that 
>> walk you through Tkinter application creation, for example,Lutz's 
>> "Programming Python". 
> 
> 
> Sorry, but I do not see the answers to any of the above
> questions, not even the first one.  Do you?  (One might
> believe question 2 is answered, but if you read it, I
> think you will see why I do not.)
> 
> Cheers,
> Alan Isaac


I think you need a bit of background (if not, just ignore this post):

1. Tkinter is only a thin wrapper over Tk, a GUI library initially 
developed for Tcl language, so many of the answer to the design choices 
you question (e.g. what is the master) cannot between answered within 
the python documentation but should be searched in the Tcl/Tk 
documentation. So google for tcl/tk.
Anyway, all GUI libraries I know of build the GUI as a hierarchical
structure. The master (often called root) ia the root of this
hierarchy, and you have to build it before building the rest.

2. Another good source of information is the source of Tkinter itself, 
which is mostly in the Tkinter.py file. This is available in your python
installation, so dig into it: if you for instance look at the __init__ 
method of the Variable class (which is the basic class of StringVar), 
you will easily find the 'magic' to which you refer to.
If you don't like the choices which have been made there (e.g not
automagically creatinga a master for variables but doing it for frames 
), you could try and submit a  patch :-)

3. Usually the changes between one version of python and the next are 
documented (but not in all gory details) in "What is new" documents you 
can find  in python.org site. I'm not aware of any change for Tkinter,
in Python 3.0 but go read it yourself. If you want more details  you 
could always fetch the Tkinter.py file (or others) of bot versions and
make a diff.

Ciao
------
FB



More information about the Python-list mailing list