2016-10-01 6 views
0

もう一度私です:) 私は自分のゲームプロジェクトを続けています。 私はテーブルを持っています(テーブルは4つの要素からなる4つのリストですが、長さは柔軟ですが、これは質問の単なる例です)。私はキャンバスの左側に4つの正方形の列を取得し、今のようテーブル(リストのリスト)に基づいてtkinterキャンバスに四角形のテーブルを描画するにはどうすればよいですか?

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() 

: はそうここに私のコードです。すべての私の試練は、他の12の四角形を作成することに失敗しました。

ありがとうございます!

+1

'for'ループ内に' for'ループを置いてみてください。外側のループは列用であり、内側のループは列の1つの中の各行用です。 – jcfollower

+1

「他の12の四角形」はどういう意味ですか? 'l'は長さが4なので' n'は4です。 'l'の目的は何ですか?あなたが投稿したコードは、その長さの計算を除いて、それを使用しません。 –

+0

私はそうしようとしましたが、それほど変化はありませんでした。私は "j"を使ってみましたが、 "j"をどこで使うべきかは分かりません。 – Chihab

答えて

1

キャンバスに正方形の四角形を描画する方法は次のとおりです。

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() 
関連する問題