我如何使用按钮创建一个棋盘,其中每个方块都有一个定义的 x 和 y 值?
How can i create a chess board using buttons where each square has a defined x and y value?
如何在此 canvas 之上为每个方块创建按钮,每个方块都有自己独特的值(从左下角的 (1,1) 开始)?我正在尝试制作一个下棋程序。我需要 canvas 上的这些方块,每个方块都有一个具有定义坐标的按钮。这也可以更改为仅 64 个按钮,但这是针对一个程序,如果方块可以移动到该选项,则方块将突出显示。
import tkinter as tk
class Layout(tk.Tk):
colours = ["#563a12", "#9f9362"]#square colours dark then light
def __init__(self, n=8):
super().__init__()
self.n = n
self.leftframe = tk.Frame(self)
self.leftframe.grid(row=0, column=0, rowspan=10, padx=100)
self.middleframe = tk.Frame(self)
self.middleframe.grid(row=0, column=8, rowspan=8)
self.canvas = tk.Canvas(self, width=1200, height=768, )
self.canvas.grid(row=0, column=1, columnspan=8, rowspan=8)
self.board = [[None for row in range(n)] for col in range(n)]
self.colourindex = 0
def changecolours(self):
self.colourindex = (self.colourindex + 1) % 2
def drawboard(self):
for col in range(self.n):
self.changecolours()
for row in range(self.n):
x1 = col * 90
y1 = (7-row) * 90
x2 = x1 + 90
y2 = y1 + 90
colour = self.colours[self.colourindex]
self.board[row][col] = self.canvas.create_rectangle(x1, y1, x2, y2, fill=colour)
self.changecolours()
board = Layout()
board.drawboard()
board.mainloop()
看来您只需要棋盘图块的坐标。如果是这样,您可以使用唯一名称标记每个图块,然后使用 canvas.tag_bind
:
绑定它们
import tkinter as tk
class Layout(tk.Tk):
colours = ["#563a12", "#9f9362"]#square colours dark then light
def __init__(self, n=8):
super().__init__()
self.n = n
self.leftframe = tk.Frame(self)
self.leftframe.grid(row=0, column=0, rowspan=10, padx=100)
self.middleframe = tk.Frame(self)
self.middleframe.grid(row=0, column=8, rowspan=8)
self.canvas = tk.Canvas(self, width=1200, height=768, )
self.canvas.grid(row=0, column=1, columnspan=8, rowspan=8)
self.board = [[None for row in range(n)] for col in range(n)]
def drawboard(self):
from itertools import cycle
for col in range(self.n):
color = cycle(self.colours[::-1] if not col % 2 else self.colours)
for row in range(self.n):
x1 = col * 90
y1 = (7-row) * 90
x2 = x1 + 90
y2 = y1 + 90
self.board[row][col] = self.canvas.create_rectangle(x1, y1, x2, y2, fill=next(color), tags=f"tile{col+1}{row+1}")
self.canvas.tag_bind(f"tile{col+1}{row+1}","<Button-1>", lambda e, i=col+1, j=row+1: self.get_location(e,i,j))
def get_location(self, event, i, j):
print (i, j)
board = Layout()
board.drawboard()
board.mainloop()
如何在此 canvas 之上为每个方块创建按钮,每个方块都有自己独特的值(从左下角的 (1,1) 开始)?我正在尝试制作一个下棋程序。我需要 canvas 上的这些方块,每个方块都有一个具有定义坐标的按钮。这也可以更改为仅 64 个按钮,但这是针对一个程序,如果方块可以移动到该选项,则方块将突出显示。
import tkinter as tk
class Layout(tk.Tk):
colours = ["#563a12", "#9f9362"]#square colours dark then light
def __init__(self, n=8):
super().__init__()
self.n = n
self.leftframe = tk.Frame(self)
self.leftframe.grid(row=0, column=0, rowspan=10, padx=100)
self.middleframe = tk.Frame(self)
self.middleframe.grid(row=0, column=8, rowspan=8)
self.canvas = tk.Canvas(self, width=1200, height=768, )
self.canvas.grid(row=0, column=1, columnspan=8, rowspan=8)
self.board = [[None for row in range(n)] for col in range(n)]
self.colourindex = 0
def changecolours(self):
self.colourindex = (self.colourindex + 1) % 2
def drawboard(self):
for col in range(self.n):
self.changecolours()
for row in range(self.n):
x1 = col * 90
y1 = (7-row) * 90
x2 = x1 + 90
y2 = y1 + 90
colour = self.colours[self.colourindex]
self.board[row][col] = self.canvas.create_rectangle(x1, y1, x2, y2, fill=colour)
self.changecolours()
board = Layout()
board.drawboard()
board.mainloop()
看来您只需要棋盘图块的坐标。如果是这样,您可以使用唯一名称标记每个图块,然后使用 canvas.tag_bind
:
import tkinter as tk
class Layout(tk.Tk):
colours = ["#563a12", "#9f9362"]#square colours dark then light
def __init__(self, n=8):
super().__init__()
self.n = n
self.leftframe = tk.Frame(self)
self.leftframe.grid(row=0, column=0, rowspan=10, padx=100)
self.middleframe = tk.Frame(self)
self.middleframe.grid(row=0, column=8, rowspan=8)
self.canvas = tk.Canvas(self, width=1200, height=768, )
self.canvas.grid(row=0, column=1, columnspan=8, rowspan=8)
self.board = [[None for row in range(n)] for col in range(n)]
def drawboard(self):
from itertools import cycle
for col in range(self.n):
color = cycle(self.colours[::-1] if not col % 2 else self.colours)
for row in range(self.n):
x1 = col * 90
y1 = (7-row) * 90
x2 = x1 + 90
y2 = y1 + 90
self.board[row][col] = self.canvas.create_rectangle(x1, y1, x2, y2, fill=next(color), tags=f"tile{col+1}{row+1}")
self.canvas.tag_bind(f"tile{col+1}{row+1}","<Button-1>", lambda e, i=col+1, j=row+1: self.get_location(e,i,j))
def get_location(self, event, i, j):
print (i, j)
board = Layout()
board.drawboard()
board.mainloop()