如何在 tkinter Python 中为 var 指定不同的名称



我想创建多个圆圈,并为每个圆圈提供不同的id,由名称表示。有什么办法可以做到这一点吗?

for i in range(7):
    #I want it to be something like col_1, col_2 etc
    col_i = tkinter.Canvas(window, width=50, height=300, bg='white')
    for j in range(6):
        col_i.create_oval(1,j*50 + 1, 52, j*50 + 52, width=3,
                        fill='white', tag = str(i) + "_" + str(j))
    col_i.grid(column = i, row = 0)
    index = str(i)
    button_i = tkinter.Button(window, command=(lambda :circle_fill(index)),
                              text="b" +str(i + 1)).grid(column=i, row=8)

为什么不直接使用列表?

cols = []
buttons = []
for i in range(7):
    cols.append(tkinter.Canvas(window, width=50, height=300, bg='white'))
    for j in range(6):
        cols[i].create_oval(1,j*50 + 1, 52, j*50 + 52, width=3,
                            fill='white', tag = str(i) + "_" + str(j))
    cols[i].grid(column = i, row = 0)
    index = str(i)
    buttons.append(tkinter.Button(window, command=(lambda: circle_fill(index)),
                                  text="b" +str(i + 1)).grid(column=i, row=8)

最新更新