T
T
thelastofme2020-09-22 17:22:51
Python
thelastofme, 2020-09-22 17:22:51

How to draw multiple images in Python using tkinter?

The essence of the project is chess. The first stage, that is, the drawing of the board, was successful:
5f6a063947346778386673.png
Next, you need to draw the figures, 80x80 pictures of which I threw into the root folder.
The pieces must be drawn based on the cells of the pieces matrix , which at the beginning of the game


where the first letter (b/w) is the color and K, Q, R, B, N or P is the piece (or pawn) .

Actually, the drawing of the figures looks like this:
for i in range(8):
        for j in range(8):
            x = 50 + i * 80     # 50 - отступ, 80х80 - размер клетки
            y = 50 + j * 80
            if pieces[i][j] == 'wP':
                img = PhotoImage(file="WhitePawn.png")
            elif pieces[i][j] == 'wK':
                img = PhotoImage(file="WhiteKing.png")
            elif pieces[i][j] == 'wQ':
                img = PhotoImage(file="WhiteQueen.png")
            elif pieces[i][j] == 'wR':
                img = PhotoImage(file="WhiteRook.png")
            elif pieces[i][j] == 'wN':
                img = PhotoImage(file="WhiteKnight.png")
            elif pieces[i][j] == 'wB':
                img = PhotoImage(file="WhiteBishop.png") 
            elif pieces[i][j] == 'bP':
                img = PhotoImage(file="BlackPawn.png")
            elif pieces[i][j] == 'bK':
                img = PhotoImage(file="BlackKing.png")
            elif pieces[i][j] == 'bQ':
                img = PhotoImage(file="BlackQueen.png")
            elif pieces[i][j] == 'bR':
                img = PhotoImage(file="BlackRook.png")
            elif pieces[i][j] == 'bN':
                img = PhotoImage(file="BlackKnight.png")
            elif pieces[i][j] == 'bB':
                img = PhotoImage(file="BlackBishop.png")
            canvas.create_image(x,y, anchor=NW, image=img)
            print(x, ' ', y, ' ', img) #отладка
            img = ''

Pictures are in place:
5f6a079239cb5855877782.png
but nothing is drawn. Moreover, if you try to draw any one figure, there are no problems.

Actually, the question is what to do? How to draw 32 pictures?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-09-22
@thelastofme

all because you have one img variable and you need an array equal to the number of cells - 8*8.
or pass the file name and not the image object.
The problem is that the variable limits the number of possible objects to 1, since a reference to the object is passed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question