0

I have a GUI with a x-number of entries. I want the output to be like a list of all the entries. I have the following code:

from Tkinter import *
master = Tk()
lijst=[]
x=0
while x<3:
    a="e"+str(x)
    lijst.append(a)
    x=x+1
x=0

labels=[]
x=1
while x<4:
    a="File"+str(x)+":"
    labels.append(a)
    x=x+1

x=0
while x<3:
    a=labels[x]
    b=Label(master,text=a)
    b.grid(row=x+1, column=0)

    x=x+1

x=0
while x<3:
    a=lijst[x]
    b=Entry(master)
    b.grid(row=x+1, column=1)
    c=Label(master, text=".txt            ")
    c.grid(row=x+1, column=1,sticky=E)
    x=x+1

Button(master, text='Enter', command=???,width=20).grid(row=4, column=2, sticky=W, pady=4,padx=20)

mainloop()   

output: list=[e0.get(),e1.get(),etc...

How can i create a list that looks like output and does not depend on the number of entries?

Glenn
  • 69
  • 9

1 Answers1

1

You can create the list of entries more easily using a comprehension list:

entries = [Entry(master) for i in range(3)]

for i, entry in enumerate(entries):
    label_text = "File%s:" % (i+1)
    Label(master, text=label_text).grid(row=i, column=0)
    entry.grid(row=i, column=1)
    Label(master, text=".txt").grid(row=i, column=2, padx=(0, 15))

Once thit list is created, print the get() call of each entry is trivial:

def print_entries():
    print [entry.get() for entry in entries]

Button(master, text='Enter', width=20, command=print_entries).grid(row=4, column=3, sticky=W, pady=4,padx=20)

I have replaced the trailing withespaces of the ".txt" string with right padding as explained here, which is more clear.

Community
  • 1
  • 1
A. Rodas
  • 20,171
  • 8
  • 62
  • 72
  • Okay thanks for refining and improving my function, but I need that output list. So I want to assign that list to a variable. So I can use that list for further definitions. How can i do that? – Glenn Mar 23 '13 at 17:18
  • @Glenn If you want to do that, it is as simple as `a = [entry.get() ...]`. Keep in mind that if `a` is a global variable, you should declare `global a` before using it. – A. Rodas Mar 23 '13 at 17:21
  • @Glenn Glad it helped. Please remember to accept the answers that solved your questions to make other users know that they were useful :) – A. Rodas Mar 23 '13 at 17:26