按钮布局未在框架中形成棋盘
Buttons layout not forming chessboard in frame
我正在制作一个国际象棋程序,我正在尝试使用网格方法来制作棋盘中的画板功能。但是布局不正常,它目前显示前两行,但行之间有很大的差距(我假设其他 6 行输出在 window 底部下方)有什么想法吗?提前致谢
import tkinter as tk#imports the gui
class Layout(tk.Tk):
colours = ["#563a12", "#9f9362"]#square colours dark then light
def __init__(self, n=8):
super().__init__()
self.n = n
self.middleframe = tk.Frame(self, )
self.middleframe.grid(row=0, column=3, rowspan=8, columnspan=8)
self.canvas = tk.Canvas(self, width=1200, height=768, )
self.canvas.grid(row=0, column=1, columnspan=8, rowspan=8)
self.colourindex = 0
self.square= (0,0)
self.promotefont=("Segoe UI Symbol", 35)
self.piecefont=("Segoe UI Symbol", 30)
self.newgame=tk.Button(self, text="New Game", font=("Segoe UI", 15), command=self.drawboard)
self.newgame.grid(row=0, column=0)
def drawboard(self):
x=0
y=0
for column in range(self.n):
self.changecolours()
x=x+1
y=0
for row in range(self.n):
y=y+1
colour = self.colours[self.colourindex]
thebuttons=(tk.Button(self.middleframe, text="♚", bg=colour, borderwidth=2, relief="solid", font=self.piecefont, ))
thebuttons.grid(column=(x-1), row=(y-1))
self.changecolours()
def changecolours(self):
self.colourindex = (self.colourindex + 1) % 2
这是因为正如 Bryan Oakley 所说,框架位于按钮之上,只需重新排列以下四行代码即可解决问题
self.canvas = tk.Canvas(self, width=1200, height=768, )
self.canvas.grid(row=0, column=1, columnspan=8, rowspan=8)
self.middleframe = tk.Frame(self, )
self.middleframe.grid(row=0, column=3, rowspan=8, columnspan=8)
我正在制作一个国际象棋程序,我正在尝试使用网格方法来制作棋盘中的画板功能。但是布局不正常,它目前显示前两行,但行之间有很大的差距(我假设其他 6 行输出在 window 底部下方)有什么想法吗?提前致谢
import tkinter as tk#imports the gui
class Layout(tk.Tk):
colours = ["#563a12", "#9f9362"]#square colours dark then light
def __init__(self, n=8):
super().__init__()
self.n = n
self.middleframe = tk.Frame(self, )
self.middleframe.grid(row=0, column=3, rowspan=8, columnspan=8)
self.canvas = tk.Canvas(self, width=1200, height=768, )
self.canvas.grid(row=0, column=1, columnspan=8, rowspan=8)
self.colourindex = 0
self.square= (0,0)
self.promotefont=("Segoe UI Symbol", 35)
self.piecefont=("Segoe UI Symbol", 30)
self.newgame=tk.Button(self, text="New Game", font=("Segoe UI", 15), command=self.drawboard)
self.newgame.grid(row=0, column=0)
def drawboard(self):
x=0
y=0
for column in range(self.n):
self.changecolours()
x=x+1
y=0
for row in range(self.n):
y=y+1
colour = self.colours[self.colourindex]
thebuttons=(tk.Button(self.middleframe, text="♚", bg=colour, borderwidth=2, relief="solid", font=self.piecefont, ))
thebuttons.grid(column=(x-1), row=(y-1))
self.changecolours()
def changecolours(self):
self.colourindex = (self.colourindex + 1) % 2
这是因为正如 Bryan Oakley 所说,框架位于按钮之上,只需重新排列以下四行代码即可解决问题
self.canvas = tk.Canvas(self, width=1200, height=768, )
self.canvas.grid(row=0, column=1, columnspan=8, rowspan=8)
self.middleframe = tk.Frame(self, )
self.middleframe.grid(row=0, column=3, rowspan=8, columnspan=8)