How do I draw a table of squares on tkinter canvas based on a table (list of lists)?
<p>it's me again :)
I'm carrying on on my game project. I'm stuck (as the beginner I am) on this thing:
I have a table (list of 4 lists of 4 elements each. The length is flexible though, this is only a simple example for the question).
so here is my code:</p>

<pre><code>from tkinter import*
l=[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
n=len(l)    #this is the length of the list l
lngt=400/len(l)    #this is the dimension of the squares that I want
fen=Tk()
fen.geometry("600x400")

#I would like to create a table of 4 rows on canvas
#each row should contain 4 squares
can=Canvas(fen,width=450,height=400,bg="lightblue")
can.pack(side=LEFT)
for i in range(n):
  can.create_rectangle(n,  i*(lngt)  ,n+lngt,  i*n+(i+1)*lngt,     fill="red")

f=Frame(fen,width=150,height=400,bg="lightcoral")
f.pack(side=LEFT)

fen.mainloop()
</code></pre>

<p>As of now, I only get a column of 4 squares on the left side of the canvas. All my trials have failed to create the 12 other squares.</p>

<p>Thank you awesome people!!</p>

<p>Here's how to draw a square grid of squares on a Canvas.</p>

<pre><code>import tkinter as tk

l = [[0,0,0,0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
n = len(l)      #this is the length of the list l
lngt = 400 // n #this is the dimension of the squares that I want

fen = tk.Tk()
fen.geometry("600x400")

#I would like to create a table of 4 rows on canvas
#each row should contain 4 squares
can = tk.Canvas(fen, width=450, height=400, bg="lightblue")
can.pack(side=tk.LEFT)

for i in range(n):
    y = i * lngt
    for j in range(n):
        x = j * lngt
        can.create_rectangle(x, y, x+lngt, y+lngt, fill="red")

f = tk.Frame(fen, width=150, height=400, bg="lightcoral")
f.pack(side=tk.LEFT)

fen.mainloop()
</code></pre>

python list canvas tkinter tkinter-canvas