How to display 2d arrays with tkinter? How to display 2d arrays with tkinter? tkinter tkinter

How to display 2d arrays with tkinter?


One easy way of displaying the buttons/labels created in the above code would be using the below code just before window.mainloop():

for i, block_row in enumerate(maze):    for j, block in enumerate(block_row):        block.grid(row=i, column=j)

Also, note that tkinter needs images to have global references which the images you create haven't, further fix the code by adding global references to your images by modifying your methods to:

def visblock():    block = tkinter.Label(window)    block.image = tkinter.PhotoImage(file="Player_Icon.png")    block['image'] = block.image    return block# These act like wallsdef invisblock():    block = tkinter.Button(window)    block.image = tkinter.PhotoImage(file="Player_Icon_Cover.png")    block['image'] = block.image    return block# These act like empty spaces"""

The entirety of the code should look like:

import tkinterwindow = tkinter.Tk()def visblock():    block = tkinter.Label(window)    block.image = tkinter.PhotoImage(file="Player_Icon.png")    block['image'] = block.image    return block# These act like wallsdef invisblock():    block = tkinter.Button(window)    block.image = tkinter.PhotoImage(file="Player_Icon_Cover.png")    block['image'] = block.image    return block# These act like empty spaces"""maze = [[visblock(), visblock(), visblock(), visblock()],        [visblock(), invisblock(), invisblock(), visblock()],        [invisblock(), invisblock(),visblock(), invisblock()],        [visblock(), invisblock(), invisblock(), invisblock()],        [visblock(),visblock(), visblock(), visblock()]]for i, block_row in enumerate(maze):    for j, block in enumerate(block_row):        block.grid(row=i, column=j)window.mainloop()