Вот небольшой тест, который работает для меня:
import tkinter as tk
import random
from PIL import ImageTk, Image
PATH_DICT = {
"A": "icons/A.png",
"B": "icons/B.png",
"C": "icons/C.png"
}
root = tk.Tk()
def randomize_board():
letters = [
['A', 'A', 'A', 'B'],
['A', 'A', 'A', 'B'],
['A', 'A', 'A', 'B'],
['A', 'A', 'C', 'A']
]
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()
ls = [
['A', 'A', 'A', 'B'],
['A', 'A', 'A', 'B'],
['A', 'A', 'A', 'B'],
['A', 'A', 'C', 'A']
]
def create_img(frame, path_to_img):
width = 100
height = 140
img = Image.open(path_to_img)
img = img.resize((width,height), Image.ANTIALIAS)
photoImg = ImageTk.PhotoImage(img)
btn= tk.Button(frame, image=photoImg, width=width)
return btn
buttons_list = []
for x in range(4):
row_btns = []
for y in range(4):
row_btns.append(create_img(root, PATH_DICT[ls[x][y]]))
row_btns[y].grid(row=y, column=x)
buttons_list.append(row_btns)
root.mainloop()
Я не могу проверить следующий код, так как у меня нет всего необходимого, но он должен работать для вас:
import tkinter as tk
import random
from PIL import ImageTk, Image
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()
def create_img(frame, path_to_img):
width = 100
height = 140
img = Image.open(path_to_img)
img = img.resize((width,height), Image.ANTIALIAS)
photoImg = ImageTk.PhotoImage(img)
btn = tk.Button(frame, image=photoImg, bg="SkyBlue", activebackground="white",
padx=20, pady=20, command=lambda x=x, y=y: getclick(x, y))
return btn
buttons_list = []
for x in range(4):
row_btns = []
for y in range(4):
row_btns.append(create_img(root, PATH_DICT[ls[x][y]]))
row_btns[y].grid(row=y, column=x)
buttons_list.append(row_btns)
root.mainloop()