我在框架内有一个画布,我说过画布应该是 250x250。但由于某种原因,它被创建得更大,右侧和底部有额外的空间。这是我的代码......有什么想法吗?
from tkinter import *
from tkinter import ttk
from player import player0
alpha = ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z')
class GUI(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.boardsize = 250
self.sqsize = self.boardsize//5
master.title("Canvas with extra space")
self.initialdraw()
self.grid(row=0,column=0)
def initialdraw(self):
mainframe = ttk.Frame(self, padding=(5,5,5,5))
mainframe.grid(column=0, row=0, sticky=(N, S, E, W))
self.board = Canvas(mainframe, width=self.boardsize, height=self.boardsize,bg='white')
self.board.grid(row=1,column=0)
for row in range(5):
for col in range(5):
top = row * self.sqsize
left = col * self.sqsize
bottom = row * self.sqsize + self.sqsize -2
right = col * self.sqsize + self.sqsize -2
rect = self.board.create_rectangle(left,top,right,bottom,outline='gray',fill='')
self.board.focus_set()
if __name__ == '__main__':
tk = Tk()
gui = GUI(tk)
tk.mainloop()
结果是这样的:
在绘制较小的正方形后,我宁愿避免画布右侧和底部的白色排水沟。