Automatic naming?

Garth Dighton gdighton at geocities.com
Wed Aug 2 18:06:08 EDT 2000


athos84 at my-deja.com wrote in <8m46a1$fr$1 at nnrp1.deja.com>:

>I am trying to create a window in Tkinter that has
>a variable number of entry fields, depending on
>the length of a list.  The script looks like this:
>
>cnt = 0
>for x in list:
>     label = Label(window, text=x)
>     entry = Entry(window)
>     label.grid(row=cnt)
>     entry.grid(row=cnt, column=1)
>     cnt = cnt+1
>
>The window looks fine, but my problem is that I
>don't know how to use .get() commands to call back
>the contents of the widget.  Is there a way to
>automatically name each entry as it gets created
>so that I can call them back when I need to?
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.

Put the entry widgets in a list, and access them that way:

import Tkinter
Tk = Tkinter

root = Tk.Tk()
entrylist = []


def MakeWidget(list):
    global entrylist
    cnt = 0

    for x in list:
        label = Tk.Label(root, text = x)
        entry = Tk.Entry(root)
        entrylist.append(entry)
        label.grid(row = cnt)
        entry.grid(row = cnt, column=1)
        cnt = cnt + 1
    button = Tk.Button(root, text = "Do It!", command = DoIt)
    button.grid(row = cnt)

def DoIt():
    for x in entrylist:
        print x.get()
    root.destroy()


MakeWidget(['spam', 'eggs', 'ham'])
root.mainloop()

-- 
Garth Dighton
Also a newbie


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list