0

I'm trying to build a chess game in Python using tkinter for graphics. I've created a grid that is 8x8 (rows and columns) and placed Tiles there (which is a type of button). It all works great, until you fill an entire row with chess pieces, then the entire row dissappears. And I have no idea why.

When filling entire row except one: https://gyazo.com/865aed481e4a84c5e9e8918695410006

When filling entire row: https://gyazo.com/acf48c9790335ba11913b6017d2652c3

As you can see, the entire row of buttons dissappear^

Here's my code:

Code for the Tile class (inherits tkinter button)

class Tile(tk.Button):
    piece = None
    imageLocation = {"bb":"./img/blackBishop.gif",
                     "bk":"./img/blackKing.gif",
                     "bh":"./img/blackHorse.gif",
                     "bp":"./img/blackPawn.gif",
                     "bq":"./img/blackQueen.gif",
                     "br":"./img/blackRook.gif",
                     "wb":"./img/whiteBishop.gif",
                     "wk":"./img/whiteKing.gif",
                     "wh":"./img/whiteHorse.gif",
                     "wp":"./img/whitePawn.gif",
                     "wq":"./img/whiteQueen.gif",
                     "wr":"./img/whiteRook.gif",
    }
    
    def __init__(self, master, *args, **kwargs):
        super(Tile, self).__init__(master, *args, **kwargs)

    def addPiece(self, pieceName):
        self.piece = pieceName
        im = Image.open(self.imageLocation[pieceName])
        photo = ImageTk.PhotoImage(im)
        self.image = photo
        self.config(image=photo)

Code for the main class:

def main():
    root = tk.Tk()
    root.title("Chess in Python using tkinter")
    root.geometry("640x640")
    root.resizable(False, False)
    board = Board(root)
    root.mainloop()

if __name__ == "__main__":
    main()

Code for the board:

class Board(tk.Frame):
    move = 0 #
    # Columns and rows
    columns = 8
    rows = 8
    # Size of a square
    square = 75
    
    # A dictionary containing all the tiles
    board = np.empty((8, 8), dtype=Tile)

    def __init__(self, master, *args, **kwargs):
        super(Board, self).__init__(master, *args, **kwargs)
        self.master = master

        self.draw_board()

        self.initialPlacement()

    def draw_board(self):
        grey = "#7d827e"
        # Create the grid
        self.master.columnconfigure((0, 1, 2, 3, 4, 5, 6, 7), weight=1)
        self.master.rowconfigure((0, 1, 2, 3, 4, 5, 6, 7), weight=1)

        # Create the tiles (as buttons)
        for row in range(self.rows):
            for col in range(self.columns):
                num = row + col
                if num % 2 == 0:
                    self.board[row][col] = Tile(self.master, bg="white", highlightbackground="white", height=self.square,
                                         width=self.square, borderwidth=0)
                else:
                    self.board[row][col] = Tile(self.master, bg=grey, highlightbackground=grey, height=self.square,
                                         width=self.square, borderwidth=0)
                self.board[row][col].grid(row=row, column=col)

    def addPiece(self, piece, row, col):
        tile = self.board[row][col]
        tile.addPiece(piece)
    
    def initialPlacement(self):
        initialArray = [
            ["br", "bh", "bb", "bq", "bk", "bb", "bh", "br"],
            ["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
            ["  ", "  ", "  ", "  ", "  ", "  ", "  ", "  "],
            ["  ", "  ", "  ", "  ", "  ", "  ", "  ", "  "],
            ["  ", "  ", "  ", "  ", "  ", "  ", "  ", "  "],
            ["  ", "  ", "  ", "  ", "  ", "  ", "  ", "  "],
            ["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"],
            ["wr", "wh", "wb", "wq", "wk", "wb", "wh", "wr"],
        ]

        for row in range(self.rows):
            for col in range(self.columns):
                piece = initialArray[row][col]
                if piece != "  ": #TODO
                    self.addPiece(piece, row, col)
3
  • The indentation of your code is broken in a few places. Also, please explain what debugging you've done. Have you examined the code in each step of your loops to verify the data is what you're assuming it is? What happens if you allow the window to resize, and then you resize the window? Do the images suddenly appear? What if you replace the images with text, does the text behave the same way? May 6, 2021 at 15:43
  • Thank you for your answer. It looks the same if I allow the window to resize, and resizing it just expands the way it looks (the images do not appear). I've tried manually adding pieces without the loop. And the same problem occurs, if I fill the entire row with pieces, it breaks like in the photos. It DOES work though if I use text instead of images.
    – vbill
    May 6, 2021 at 15:49
  • Hi. I just fixed the issue. In the row and column configuration, I added minsize=self.square. The problem was somehow that the tiles were shrunk to 0 pixels. When forcing the minsize to the size of the tile it worked.
    – vbill
    May 6, 2021 at 16:01

1 Answer 1

0

Adding minsize=self.square to the row and column configuration solved the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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