Python 每两行交错设置一个tkinter网格
我在这里使用这个Python 每两行交错设置一个tkinter网格,python,tkinter,grid,tkinter-canvas,Python,Tkinter,Grid,Tkinter Canvas,我在这里使用这个grid.py着色程序,生成网格的相关代码是 self.cells = [] for iy in range(n): for ix in range(n): xpad, ypad = pad * (ix+1), pad * (iy+1) x, y = xpad + ix*xsize, ypad + iy*ysize rect = self.w.create_rectangle(x, y, x+xsize,
grid.py
着色程序,生成网格的相关代码是
self.cells = []
for iy in range(n):
for ix in range(n):
xpad, ypad = pad * (ix+1), pad * (iy+1)
x, y = xpad + ix*xsize, ypad + iy*ysize
rect = self.w.create_rectangle(x, y, x+xsize,
y+ysize, fill=UNFILLED)
self.cells.append(rect)
我想知道我是否可以得到它,使方块交错如下所示:
您可以使用生成器函数为每条线生成坐标和每个单元格的填充。
注
行%4<2
:这是为连续行生成相同填充图案的原因,这些行您可以根据
网格类
轻松地调整以下代码示例
将tkinter作为tk导入
宽度,高度=300300
def管路_填充(行,numcells):
“”“产生一行中每个单元格的填充值,从
行的行位置
"""
如果行%4<2,则填充=真,否则为假#注意%4:这是为连续行和行生成相同填充模式的原因
对于范围内的u(numcells):
屈服填方
填充=不填充
def管路坐标(世界其他地区、numcells、侧面):
“”“生成一行中每个单元格的矩形坐标,从
行的行位置
"""
y0,y1=行*边,(行+1)*边
对于范围内的列(numcells):
x0,x1=列*边,(列+1)*边
屈服强度x0,y0,x1,y1
def交错网格(画布):
“”“在画布上生成并绘制交错网格
"""
侧面=10
w、 h=宽度//侧面+1,高度//侧面+1
填充=['',灰色']
对于范围(h)中的行:
对于坐标,填写zip(线坐标(行,w,侧),线坐标(行,w)):
填充='灰色',如果填充其他''
画布。创建矩形(*坐标,填充=填充)
root=tk.tk()
canvas=tk.canvas(根,宽度=宽度,高度=高度)
canvas.pack()
交错网格(画布)
root.mainloop()
您可以使用生成器函数为每一行生成坐标和每个单元格的填充。注
行%4<2
:这是为连续行生成相同填充图案的原因,这些行您可以根据
网格类
轻松地调整以下代码示例
将tkinter作为tk导入
宽度,高度=300300
def管路_填充(行,numcells):
“”“产生一行中每个单元格的填充值,从
行的行位置
"""
如果行%4<2,则填充=真,否则为假#注意%4:这是为连续行和行生成相同填充模式的原因
对于范围内的u(numcells):
屈服填方
填充=不填充
def管路坐标(世界其他地区、numcells、侧面):
“”“生成一行中每个单元格的矩形坐标,从
行的行位置
"""
y0,y1=行*边,(行+1)*边
对于范围内的列(numcells):
x0,x1=列*边,(列+1)*边
屈服强度x0,y0,x1,y1
def交错网格(画布):
“”“在画布上生成并绘制交错网格
"""
侧面=10
w、 h=宽度//侧面+1,高度//侧面+1
填充=['',灰色']
对于范围(h)中的行:
对于坐标,填写zip(线坐标(行,w,侧),线坐标(行,w)):
填充='灰色',如果填充其他''
画布。创建矩形(*坐标,填充=填充)
root=tk.tk()
canvas=tk.canvas(根,宽度=宽度,高度=高度)
canvas.pack()
交错网格(画布)
root.mainloop()
以下是基于您的代码的示例(添加缺少的部分):
将tkinter作为tk导入
类应用程序(tk.tk):
定义初始化(自):
super()。\uuuu init\uuuuu()
###添加缺少的部件
pad=0
xsize=ysize=30
n=8
self.w=tk.Canvas(self,宽度=(xsize+pad)*n+pad+1,高度=(ysize+pad)*n+pad+1,highlightthickness=0)
self.w.pack()
###
self.cells=[]
对于范围(n)内的iy:
对于范围(n)内的ix:
xpad,ypad=pad*(ix+1),pad*(iy+1)
x、 y=xpad+ix*xsize,ypad+iy*ysize
颜色=“白色”如果(iy//2+ix)%2其他为“灰色”
rect=self.w.创建_矩形(x,y,x+xsize,y+ysize,fill=color)#使用颜色而不是未填充
self.cells.append(rect)
app=app()
app.mainloop()
结果是:
以下是基于您的代码的示例(添加缺少的部分):
将tkinter作为tk导入
类应用程序(tk.tk):
定义初始化(自):
super()。\uuuu init\uuuuu()
###添加缺少的部件
pad=0
xsize=ysize=30
n=8
self.w=tk.Canvas(self,宽度=(xsize+pad)*n+pad+1,高度=(ysize+pad)*n+pad+1,highlightthickness=0)
self.w.pack()
###
self.cells=[]
对于范围(n)内的iy:
对于范围(n)内的ix:
xpad,ypad=pad*(ix+1),pad*(iy+1)
x、 y=xpad+ix*xsize,ypad+iy*ysize
颜色=“白色”如果(iy//2+ix)%2其他为“灰色”
rect=self.w.创建_矩形(x,y,x+xsize,y+ysize,fill=color)#使用颜色而不是未填充
self.cells.append(rect)
app=app()
app.mainloop()
结果是:
import tkinter as tk
WIDTH, HEIGHT = 300, 300
def line_fills(row, numcells):
"""yields the fill value of each cells of a line, from
the row position of the line
"""
fill = True if row % 4 < 2 else False # note %4: this is what generates identical fill pattern for consecutive rows the rows
for _ in range(numcells):
yield fill
fill = not fill
def line_coords(row, numcells, side):
"""yields the rectangle coordinates of each cells in a line, from
the row position of the line
"""
y0, y1 = row * side, (row+1) * side
for col in range(numcells):
x0, x1 = col * side, (col+1) * side
yield x0, y0, x1, y1
def staggered_grid(canvas):
"""generates and draws a staggered grid on a canvas
"""
side = 10
w, h = WIDTH // side + 1, HEIGHT // side + 1
fills = ['', 'grey']
for row in range(h):
for coord, fill in zip(line_coords(row, w, side), line_fills(row, w)):
fill = 'grey' if fill else ''
canvas.create_rectangle(*coord, fill=fill)
root = tk.Tk()
canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT)
canvas.pack()
staggered_grid(canvas)
root.mainloop()