Python 3.x 如何使用按钮创建一个棋盘,其中每个方块都有一个定义的x和y值?
如何在画布顶部为每个正方形创建按钮,每个按钮都有自己的唯一值(从左下角的(1,1)开始)?我正在尝试做一个程序来下棋。我需要画布上的这些方块都有一个带有定义坐标的按钮。这也可以更改为只有64个按钮,但这是一个程序,其中方块将突出显示,如果他们有移动到的选项Python 3.x 如何使用按钮创建一个棋盘,其中每个方块都有一个定义的x和y值?,python-3.x,button,tkinter,tkinter-canvas,Python 3.x,Button,Tkinter,Tkinter Canvas,如何在画布顶部为每个正方形创建按钮,每个按钮都有自己的唯一值(从左下角的(1,1)开始)?我正在尝试做一个程序来下棋。我需要画布上的这些方块都有一个带有定义坐标的按钮。这也可以更改为只有64个按钮,但这是一个程序,其中方块将突出显示,如果他们有移动到的选项 将tkinter作为tk导入 班级布局(tk.tk): 颜色=[“#563a12”、“#9f9362”]#方形颜色先暗后亮 定义初始化(自,n=8): super()。\uuuu init\uuuuu() self.n=n self.left
将tkinter作为tk导入
班级布局(tk.tk):
颜色=[“#563a12”、“#9f9362”]#方形颜色先暗后亮
定义初始化(自,n=8):
super()。\uuuu init\uuuuu()
self.n=n
self.leftframe=tk.Frame(self)
self.leftframe.grid(行=0,列=0,行跨度=10,padx=100)
self.middleframe=tk.Frame(self)
self.middleframe.grid(行=0,列=8,行跨度=8)
self.canvas=tk.canvas(self,宽度=1200,高度=768,)
self.canvas.grid(行=0,列=1,列span=8,行span=8)
self.board=[[范围(n)中的行无](n)中的列无]
self.colorIndex=0
def更换颜色(自身):
self.colorIndex=(self.colorIndex+1)%2
def牵引板(自):
对于范围内的列(self.n):
self.changecolors()
对于范围内的行(self.n):
x1=col*90
y1=(7排)*90
x2=x1+90
y2=y1+90
颜色=自身颜色[自身颜色索引]
self.board[row][col]=self.canvas.create_矩形(x1,y1,x2,y2,fill=color)
self.changecolors()
board=布局()
木板
board.mainloop()
看起来您只需要板瓷砖的坐标。如果是这样,您可以使用唯一的名称标记每个磁贴,然后使用canvas.tag\u 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()
将tkinter作为tk导入
班级布局(tk.tk):
颜色=[“#563a12”、“#9f9362”]#方形颜色先暗后亮
定义初始化(自,n=8):
super()。\uuuu init\uuuuu()
self.n=n
self.leftframe=tk.Frame(self)
self.leftframe.grid(行=0,列=0,行跨度=10,padx=100)
self.middleframe=tk.Frame(self)
self.middleframe.grid(行=0,列=8,行跨度=8)
self.canvas=tk.canvas(self,宽度=1200,高度=768,)
self.canvas.grid(行=0,列=1,列span=8,行span=8)
self.board=[[范围(n)中的行无](n)中的列无]
def牵引板(自):
从itertools导入周期
对于范围内的列(self.n):
颜色=循环(self.colors[:-1]如果不是列%2,则为self.colors)
对于范围内的行(self.n):
x1=col*90
y1=(7排)*90
x2=x1+90
y2=y1+90
self.board[row][col]=self.canvas.create_矩形(x1,y1,x2,y2,fill=next(颜色),tags=f“tile{col+1}{row+1}”)
self.canvas.tag_bind(f“tile{col+1}{row+1}”,λe,i=col+1,j=row+1:self.get_位置(e,i,j))
def获取位置(自身、事件、i、j):
打印(i,j)
board=布局()
木板
board.mainloop()
您的程序是做什么的,它与您期望它做的有何不同?