我想在Tkinter中使用for循环每侧绘制8帧。首先是白色,然后是黑色(如棋盘)。
from tkinter import *
window = Tk()
window.title("pyChess")
window.geometry("523x523+250+0")
window.configure( background = 'brown')
x1 = 0
y1 = 0
x2 = 65
y2 = 65
j1 = 65
k1 = 0
j2 = 130
k2 = 65
for i in range(8):
i = Canvas(window, width=520, height=1000)
i.create_rectangle(x1, y1, x2, y2, fill="white")
x1 += 130
x2 += 130
i.create_rectangle(j1, k1, j2, k2, fill="black")
j1 += 130
j2 += 130
i.pack()
window.mainloop()
我写了这段代码,但没有给我八帧。它只是打开了一个空白屏幕。我该怎么办?
答案 0 :(得分:0)
您不需要为每个Canvas
小部件更新 x1,x2,j1,j2 值。您可以在相同画布上绘制所有矩形(因此需要更新x1,x2,j1和j2的值),或者在每行上网格多个Canvas窗口小部件。
尝试以下代码(矩形仅在第一行中绘制):
from tkinter import *
window = Tk()
window.title("pyChess")
window.geometry("523x523+250+0")
window.configure( background = 'grey')
x1 = 0
y1 = 0
x2 = 65
y2 = 65
j1 = 65
k1 = 0
j2 = 130
k2 = 65
a = [0 , 1, 2, 3]
for x,i in enumerate(a):
i = Canvas(window, width=130, height=65)
i.create_rectangle(x1, y1, x2, y2, fill="white")
i.create_rectangle(j1, k1, j2, k2, fill="black")
i.grid(row = 0, column = x)
window.mainloop()