We are creating a Boggle game for school, and we want to make the interface as as nice as possible. We created an icon for each letter and we want each button to have it's icon. For some reason is doesnt work, and I can't figure out why - it only designate an image for the last button (as shown in the picture). This is my code:
import tkinter as tk
import random
import os
PATH_DICT = {"A": "\icons\A.png", "B": "\icons\B.png", "C": "\icons\C.png",
"D": "\icons\D.png", "E": "\icons\E.png", "F": "\icons\F.png",
"G": "\icons\G.png", "H": "\icons\H.png", "I": "\icons\I.png",
"J": "\icons\J.png", "K": "\icons\K.png", "L": "\icons\L.png",
"M": "\icons\M.png", "N": "\icons\MN.png", "O": "\icons\O.png",
"P": "\icons\P.png", "Qu": "\icons\Qu.png", "R": "\icons\R.png",
"S": "\icons\S.png", "T": "\icons\T.png", "U": "\icons\TU.png",
"V": "\icons\J.png", "W": "\icons\W.png", "X": "\icons\X.png",
"Y": "\icons\Y.png", "Z": "\icons\Z.png"}
root = tk.Tk()
def randomize_board():
letters = [
['A', 'E', 'A', 'N', 'E', 'G'],
['A', 'H', 'S', 'P', 'C', 'O'],
['A', 'S', 'P', 'F', 'F', 'K'],
['O', 'B', 'J', 'O', 'A', 'B'],
['I', 'O', 'T', 'M', 'U', 'C'],
['R', 'Y', 'V', 'D', 'E', 'L'],
['L', 'R', 'E', 'I', 'X', 'D'],
['E', 'I', 'U', 'N', 'E', 'S'],
['W', 'N', 'G', 'E', 'E', 'H'],
['L', 'N', 'H', 'N', 'R', 'Z'],
['T', 'S', 'T', 'I', 'Y', 'D'],
['O', 'W', 'T', 'O', 'A', 'T'],
['E', 'R', 'T', 'T', 'Y', 'L'],
['T', 'O', 'E', 'S', 'S', 'I'],
['T', 'E', 'R', 'W', 'H', 'V'],
['N', 'U', 'I', 'H', 'M', 'Qu']
]
board = []
for i in range(4):
row = []
for j in range(4):
letter = random.choice(letters[i * 4 + j])
row.append(letter)
board.append(row)
return board
ls = randomize_board()
CURR_DIR = os.path.dirname(os.path.abspath(__file__))
image_lst = []
for x in range(4):
for y in range(4):
i = 0
photo1 = tk.PhotoImage(file=CURR_DIR + PATH_DICT[ls[x][y]])
btn = tk.Button(image=photo1, bg="SkyBlue", activebackground="white",
padx=20, pady=20, command=lambda x=x, y=y: getclick(x, y))
btn.grid(row=y, column=x)
i += 1
root.mainloop()
What am I doing wrong? Thank you!