-1

I wrote a programs that creates labels and entry widgets with a for loop. I want to save each entry widget in an array and than access each entry widget separately (for this example I want to put the word 'apple' in each entry widget.

this is my code:

import tkinter as tk

fields = 'last name', 'first name', 'job', 'state'

def makeform (root, fields):
    frame1 = tk.frame(root)
    frame1.pack()
    entries = []
    for I in range[0,4]
       lab = tk.Label(frame1,text=fields[I]).grid(row=I, column=0)
       ent = tk.Entry(frame1).grid(row=I, column=1)
       entries.append((field[I],ent))
    return entries

if _name_=="_main_":
    root=tk.Tk()
    ents=makeform(root, fields)
    for ent in ents:
        print (ents)
        ent[1].delete(0, END)
        ent[1].insert(0, "apple")

    
     root.mainloop

and getting a 'attributeError: Nonetype object has no attribute 'delete'' and the 'print(ent[1])' command prints out none.

what am I missing? how do I save the entry widget in an array (their location?)

thank you

roganjosh
  • 12,594
  • 4
  • 29
  • 46

1 Answers1

0

Try to post code that is fully working. I had to fix a couple of errors in your code to reproduce your error (see minor fixes below).

The main issue is that you need to keep the result of tk.Entry(..) in a variable and then call ent.grid(...) because ent.grid(...) returns None, not the tk.Entry you were hoping for.

import tkinter as tk

fields = 'last name', 'first name', 'job', 'state'


def makeform(root, fields):
    frame1 = tk.Frame(root)
    frame1.pack()
    entries = []
    for I in range(0, 4):
        lab = tk.Label(frame1, text=fields[I]).grid(row=I, column=0)
        ent = tk.Entry(frame1)
        ent.grid(row=I, column=1)
        entries.append((fields[I], ent))
    return entries


if __name__ == '__main__':
    root = tk.Tk()
    ents = makeform(root, fields)
    for ent in ents:
        print(ents)
        ent[1].delete(0, tk.END)
        ent[1].insert(0, "apple")

    root.mainloop()
Glenn Gribble
  • 390
  • 1
  • 7