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)
Adding minsize=self.square to the row and column configuration solved the problem.