[Tutor] program for creation of unique objects

Alan Gauld alan.gauld at freenet.co.uk
Sun Mar 20 18:45:25 CET 2005

>   I want a user to define an item and then
>   define multiple characteristics about that item,
>   save a dictionary of items,
>   and then reload the items back into the GUI upon restart.

I'm still not absolutely sure what you mean. Do you mean
the GUI itself is going to grow new panels etc as the user
works with it? Or is it the items the GUI displays that
increase - like graphics objects within a canvas for example?

>   The code I'm have trouble with looks something like this.
> def newitem():
>     itemframe = Frame(parent)
>     entry1 = Entryfield(itemframe)
>     entry2 = Entryfield(itemframe)
>     button1 = Button(itemframe, command = update())

These variables only live for as long as the function lives.
You need to store them in eitther a global variable somewhere
(or more probably a global container somewhere) or as part
of a class from which you create multiple instances.

One of the primary advantages of OOP and classes is that
although they share code the data in distinct for each object.

> def update():
>     a = entry1.getvalue()
>     b = entry2.getvalue()
>     values = [a,b]
>     print values

Is it the list [a,b] that you want to persist? If so, you need
to save the list (instead of printing it), thus you need a global
container which will contain the [a,b] pairs. (A tuple (a,b) is
the most natural way if doing this rather than a list [a,b])

>   The problem I'm having is that as soon as the user runs newitem()
>   the previous itemframe loses its name and i can only access the
> recently
>   created itemframe.
>   I feel like the most convenient way to accomplish this would be
> like
>     x = Frame() in parent
>     itemframe'x' = itemframe

THe most conventiant way is OOP.

class myFrame(Frame):
   def __init__(self,parent=0):  # replace newitem()
      self.entry1 = Entryfield(itemframe)
      self.entry2 = Entryfield(itemframe)
      self.button1 = Button(itemframe, command = update())

   def update():
      self.a = self.entry1.getvalue()
      self.b = self.entry2.getvalue()
      print self.a, self.b

Now you can do things like:

x = MyFrame()
y = MyFRame()


and so on.

I'm still not entirely sure this is what you want. And there's
a lot of work to fix the code to be sensible Tkinter - no layout
manager calls here for example...

But OOP is almost certainly the best approach to your problem.

>   Where 'x' is a unique integer.  But I don't know how to create
> variables on the fly.

Do you mean unique variable names or unique values?
The latter can be achieved simply with a global sequence number
and a function that returns a name and increments the sequence number.
Remember to save the last value between program runs.

>   I'm sure there are a thousand ways to accomplish what I'm trying
>   and it seems like a trivial thing that gets dealt with all the

Givemn I'm not sure what you are trying to do exactly I'm not so sure
its done all the time. But it may be that you are falling into the
of trying to name your objects dynamically too? In which case rethink
the idea and use a container to store the instances instead, its much
easier long term. See my OOP page for more on OOP in general and how
to avoid dynamic variable creation using a dictionary.

Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list