[Tutor] bizare Tkinter error

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 3 May 2001 01:43:14 -0700 (PDT)


On Wed, 2 May 2001, Pijus Virketis wrote:

> I am writing a class assignment right now, and I have run into a most
> peculiar bug. I know where it is, but I have absolutely no clue why it
> occurs. Here is the relevant bit of my code:
> 
> class EditFrame:
>     def __init__(self, master):
>       ### Set up prompt frame, list frame, edit frame
>         p_f = l_f = e_f = Frame(master)


There's a part here that worries me.  p_f, l_f, and e_f all are names for
the same Frame; that is, they're just different names for the same object.

I'm assuming that you want all three names to refer to distinct things; if
so, you'll need this instead:

     p_f, l_f, e_f = Frame(master), Frame(master), Frame(master)

Every time we say "Frame(...)", we make a new Frame, distinct from the
others.  What you had before appears to only make one frame, so that might
be affecting the rest of your code.

A similar thing happens when we deal with lists:

###
>>> x = y = [1, 2, 3, 4]
>>> x
[1, 2, 3, 4]
>>> y
[1, 2, 3, 4]
>>> y[4:] = [5, 6, 7]
>>> y
[1, 2, 3, 4, 5, 6, 7]
>>> x
[1, 2, 3, 4, 5, 6, 7]        
###

We explain this by saying that 'x' and 'y' are both directed at the same
list; this is a problem if we want to treat the two independently.  There
are several ways to fix this, but one of the simpler ways of doing this is
with the copy module:

###
>>> import copy
>>> y = [1, 2, 3, 4]
>>> x = copy.copy(y)
>>> y[4:] = [5, 6, 7]
>>> y
[1, 2, 3, 4, 5, 6, 7]
>>> x
[1, 2, 3, 4] 
###

Unfortunately, copy.copy() isn't smart enough to work for Frames.  

This reference/object stuff is a somewhat advanced topic, so if it's a
little confusing, feel free to ask us more about it, and we'll see if we
can cook up a good explanation.

Hope this helps!