0

I have the following code.

from Tkinter import  *

root = Tk()
n_array = []        #will be used as 2d array

def add_four_entries():
    global root, n_array

    row_array=[]    #array used to store a row
    n_array.append(row_array)
    y=len(n_array)            

    for x in range(4):
        tbn="t"+str(y)+str(x)   #create entrybox names of the form t10, t11,...
        #print(tbn)
        tbn=Entry(root)
        row_array.append(tbn)
        row_array[x].grid(row=y, column=x,sticky="nsew", padx=2,pady=2)

def getval():
    for row in range(len(n_array)):

        for col in range(4):
            tbn="t"+str(row+1)+str(col)
            print(tbn.get())

Button(root, text="Add new row", command=add_four_entries).grid(row=0, column=1,)
Button(root, text="Print val", command=getval).grid(row=21, column=1,)
mainloop()

The button 'add new row' creates a row of four text boxes, each time it is pressed. The button 'Print Val' prints the values of all text boxes one by one. Since the text boxes are dynamically named, the names are string type. But ob clicking the get Val button I am getting this error.

AttributeError: 'str' object has no attribute 'get'

What am I doing wrong?

1 Answer 1

1

Replace

for col in range(4):
    tbn="t"+str(row+1)+str(col)
    print(tbn.get())

with

for col in range(4):
    print(n_array[row][col].get())

You don't need to store names. If the grid is built in a known order you can use the n_array indexes (actually a list of lists) as coordinates to get to the Entry controls you saved.

You are actually throwing away the tbn strings when you do

tbn=Entry(root)

but you store the Entry objects, that's why it still works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.