0

I need to access the value of a 2-d entry, so I use list of lists to contain all of the widgets. The code is shown below. It generates a simple 2x3 array as an experiment. My code can only get value of the first row. Can anybody please help? Thanks!

import tkinter as tk
from tkinter import *  

class MyWindow:
    def __init__(self, master):
        row=[]  #list used to store a row
        for i in range(2):
            for j in range(3):
                self.ent=Entry()
                row.append(self.ent)
                self.ent.grid(row=i, column=j,sticky="nsew", padx=2,pady=2)
                self.ent.insert(END, str(i+j))
            row_list.append(row)
        self.b=Button(text="Print val", command=self.getValue)
        self.b.grid(row=21, column=1)
        
    def getValue(self):
        for i in range(2):
            for j in range(3):
                print(row_list[i][j].get())


row_list = []        # list of row

window=Tk()
mywin=MyWindow(window)
window.mainloop()

neo
  • 3
  • 3

1 Answers1

1

You only ever create a single row. You need to move the initialization of row inside the outermost loop so that a new row is created for each value of i

for i in range(2):
    row=[]  #list used to store a row
    for j in range(3):
        ...
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685