0

I have two lists of two-dimensional arrays. In one list (a), I have arrays of Tkinter entries. In the other (b), I read in the entries using .get(). For some reason, all of the two-dimensional arrays in (b) are overwritten by the last indexed array (read from a). Are separate objects needed to prevent this? Here is my code:

from Tkinter import *
import ttk
print "\n"

def create_grid(x, y, data=None): # x is rows, y is columns

    empty = [None] * x # if coordinates not needed
    for i in range(x):
        empty[i] = [None] * y


    if data == 1: # if coordinates needed, adds in pairs
        for i in range(x):
            for j in range(y):
                empty[i][j] = [None] * 2



    return empty

def process(event):
    global a
    global b
    global c

    for i in range(2):
        for j in range(2):
            for k in range(2):
                b[i][j][k] = a[i][j][k].get()

    print b

w = Tk()
w.wm_title("test")


a = [create_grid(2,2)] * 2
b = [create_grid(2,2)] * 2


for i in range(2):
    for j in range(2):
        for k in range(2):
            a[i][j][k] = Entry(w)
            a[i][j][k].grid(row=(2*i)+j, column=k)

button = Button(w, text="process")
button.grid(row=8, column = 0, columnspan = 2)
button.bind("<Button-1>", process)


w.mainloop()

Entries and the resulting output

Bogasaur
  • 11
  • 1
  • Well that's embarrassing. Thank you! – Bogasaur Aug 03 '16 at 17:38
  • it shouldn't be, this is a very common mistake when you're learning Python, and one that's bit all of us more than a few times. It makes sense when you're thinking about passing by reference instead of by value but in Python that's not a distinction we often have to think about. – Adam Smith Aug 03 '16 at 17:40

0 Answers0