[Tutor] program for creation of unique objects

Kent Johnson kent37 at tds.net
Sun Mar 20 13:27:18 CET 2005

Brainerd HeadFat wrote:
> Hi,
>  I'm having trouble trying to figure out how to create multiple
>  objects that contain duplicate entries that can be uniquely identified
>  within a Tkinter GUI.
>  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.
>  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())
> def update():
>    a = entry1.getvalue()
>    b = entry2.getvalue()
>    values = [a,b]
>    print values
>  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 most 
> recently
>  created itemframe.

It sounds like maybe you should keep a list of the itemframes and their contents. One way to do this 
would be to have a list
   itemframes = []

then in newitem() save the parts you care about like this:
   itemdetails = (itemframe, entry1, entry2)

Then when you want to access all the itemframes use a loop like this:
   for itemframe, entry1, entry2 in itemframes:
     # do something with itemframe, entry1, entry2


>  I feel like the most convenient way to accomplish this would be 
> something like
>    x = Frame() in parent
>    itemframe'x' = itemframe
>  Where 'x' is a unique integer.  But I don't know how to create unique 
> variables on the fly.
>  I'm sure there are a thousand ways to accomplish what I'm trying to,
>  and it seems like a trivial thing that gets dealt with all the time,
>  and I'm embarrased that I have to ask.
>  I'm pretty new to programming and this is my first attempt at a GUI or 
> a really useful program.  Thanks for your help.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list