This is a section of code which I use to display a chess board in a window:
import tkinter as tk
from PIL import Image, ImageTk
def render():
window = tk.Tk()
window.geometry("1920x1080")
window.title("Chess 2")
frame = tk.Frame(window)
frame.pack()
canvas = tk.Canvas(frame, width=800, height=800)
canvas.pack()
boardImg = ImageTk.PhotoImage(file = r"successful projects\imgs\board.png")
canvas.create_image(400,400,image=boardImg)
whitePawnImg = ImageTk.PhotoImage(file = r"successful projects\imgs\whitePawn.png")
# ...
for y in range(len(board)):
for x in range(len(board[y])):
match board[y][x]:
case "p":
canvas.create_image(50+(x*100),50+(y*100),image=blackPawnImg)
# ...
case _:
pass
window.mainloop()
In the code, "board" is a representation of a chess board like so (shortened):
[[' ', ' '],
[' ', 'p']]
When I call render()
, everything works fine. Trouble is, when I change the list to eg.
[[' ', 'p'],
[' ', ' ']]
, then call render()
again, the window stays the same. What I want is for the board to render everything again. Any ideas on how to fix?