如何修复我的网格?

How do I fix my grid?

对于我正在创建的棋盘游戏程序,我通过使用 canvas.create_rectangle 遍历行和列来绘制棋盘。我希望它看起来像一个网格,所以每个矩形都有一个边框。

我目前拥有的是:

def draw_board(canvas, width, height, n):
    for row in range(n+1):
        for col in range(n+1):
            canvas.create_rectangle(row*height/n,col*width/n,(row+1)*height/n,(col+1)*width/n,width=0,fill='white',outline='black')

from Tkinter import *
import math

width = 500
height = 500
n = 10

window=Tk()
window.title('Color grid')

canvas=Canvas(window,width=width,height=height,highlightthickness=0)
canvas.grid(row=0,column=0,columnspan=5)

draw_board(canvas, width, height, 10)

window.mainloop()

但是,我运行程序的时候,轮廓没有出现,最终只得到一个白色window。

看看你的代码。

canvas.create_rectangle(row*height/n,col*width/n,(row+1)*height/n,(col+1)*width/n,width=0,fill='white',outline='black')

来自 TKinter 参考:

width - Width of the border. Default is 1 pixel. Use width=0 to make the border invisible.

您将宽度设置为 0。所以它是不可见的。这就是为什么您只看到白色 window.

编辑:此外,您不需要输入 outline='black',因为这是默认值。