-1

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?

3
  • Each time you call render you are creating a whole new window. Commented Jun 26, 2022 at 17:37
  • Okay, is there a way to update the current window with moved images? canvas.move? Commented Jun 26, 2022 at 17:40
  • does this stack overflow post answer your question r.e. moving images?
    – Raymi306
    Commented Jun 26, 2022 at 17:47

2 Answers 2

0

When you add things to a Canvas widget, using one of its create_xxx() methods, like create_image(), an integer id number is returned which you could use to move the object later via its move() method. That's all you have need to do to update what's being displayed, no explicit "refresh" step is needed.

For example, if you initially placed a pawn image somewhere using:

pawn_id = canvas.create_image(50+(x*100), 50+(y*100), image=blackPawnImg)

You could move the pawn image 10 pixels to the right and 20 down via (note that the movement amounts are specified relative to its current position):

canvas.move(pawn_id, 10, 20)

You can also use the more generic itemconfigure() method to change other options associated with Canvas items (such as the color of a line created with create_line()).

0

You don't need to anything to update the canvas other than to draw, redraw, delete, or move objects on the canvas. Whenever you add something to a canvas, the canvas will add an event to the event queue telling tkinter that the widget needs to be refreshed. The next time control returns to mainloop, the canvas will automatically be refreshed.

Your code has a fundamental flaw that each call to refresh will create an entirely new window, entirely new canvas, etc. Instead, your refresh should work with an existing window and canvas, and all it needs to do is move items around. The window itself will automatically refresh once the function returns.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.