Python tkinter - 填充整列或整行时按钮消失
Python tkinter - buttons disappears when filling entire column or row
我正尝试在 Python 中使用 tkinter 构建一个国际象棋游戏。
我创建了一个 8x8 的网格(行和列)并在其中放置了 Tiles(这是一种按钮)。
一切都很好,直到你用棋子填满一整行,然后整行消失。我不知道为什么。
填充除一行以外的整行时:
https://gyazo.com/865aed481e4a84c5e9e8918695410006
填充整行时:
https://gyazo.com/acf48c9790335ba11913b6017d2652c3
如你所见,整行按钮都消失了^
这是我的代码:
Tile 代码class(继承 tkinter 按钮)
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)
主要代码 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()
板代码:
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)
将 minsize=self.square 添加到行和列配置中解决了问题。
我正尝试在 Python 中使用 tkinter 构建一个国际象棋游戏。 我创建了一个 8x8 的网格(行和列)并在其中放置了 Tiles(这是一种按钮)。 一切都很好,直到你用棋子填满一整行,然后整行消失。我不知道为什么。
填充除一行以外的整行时: https://gyazo.com/865aed481e4a84c5e9e8918695410006
填充整行时: https://gyazo.com/acf48c9790335ba11913b6017d2652c3
如你所见,整行按钮都消失了^
这是我的代码:
Tile 代码class(继承 tkinter 按钮)
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)
主要代码 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()
板代码:
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)
将 minsize=self.square 添加到行和列配置中解决了问题。