调用重置功能时,仅清除条目的最后一行

时间:2019-11-19 09:21:48

标签: python python-3.x tkinter tkinter-canvas tkinter-entry

我正在练习python的基本概念,并尝试在Tkinter的帮助下对'SUDOKU'游戏进行编程。但是,当我尝试使用reset函数清除所有Entry字段时,程序发生冲突,因为只有最后一行的Entries被执行,除了整个2D列表。

def reset():
    for i in range(0,rows,1):
        for j in range(0,cols,1):
            col[i][j].delete(0, "end")

from Tkinter import Tk, Canvas, Entry, Button, CENTER, IntVar

root = Tk()

screen = Canvas(root, height = 430, width = 460)
screen.pack()

screen.create_text(200, 15, text = 'SUDOKU', font = 'calibri 20 bold')

screen.create_line(127, 54, 127, 408)
screen.create_line(232, 54, 232, 408)

screen.create_line(24, 170, 336, 170)
screen.create_line(24, 290, 336, 290)

check_button = Button(root, text = 'Check', font = 'Calibri 10 bold', bd = 0, bg = 'Green', fg = 'white', padx = 10, pady = 5)
screen.create_window(400, 70, window = check_button)

reset_button = Button(root, text = 'Reset', font = 'Calibri 10 bold', bd = 0, bg = 'Red', fg = 'white', padx = 10, pady = 5, command = reset)
screen.create_window(400, 110, window = reset_button)

x, y, n = 0, 0, 0

cols, rows, values = 9, 9, 81

col = [[None]*cols]*rows
val = [[IntVar]*cols]*rows

for i in range(0,rows,1):
    x = 0
    if i == 0:
        y += 70 
    else:
        y += 40
    for j in range(0,cols,1):
        x += 40
        col[i][j] = Entry(root, width = 2, font = 'Calibri 20', bd = 0, justify = CENTER)
        screen.create_window(x, y, window = col[i][j])
        x -= 5

root.mainloop()

1 个答案:

答案 0 :(得分:0)

构造col和val的方式将创建9行,这些行指向相同的9列。仅创建一行,然后指向9次

cols, rows, values = 9, 9, 81
col = [[None]*cols]*rows
col
# [[None, None, None, None, None, None, None, None, None],
#  [None, None, None, None, None, None, None, None, None],
#  [None, None, None, None, None, None, None, None, None],
#  [None, None, None, None, None, None, None, None, None],
#  [None, None, None, None, None, None, None, None, None],
#  [None, None, None, None, None, None, None, None, None],
#  [None, None, None, None, None, None, None, None, None],
#  [None, None, None, None, None, None, None, None, None],
#  [None, None, None, None, None, None, None, None, None]]

col[0][1]=100
col
# [[None, 100, None, None, None, None, None, None, None],
#  [None, 100, None, None, None, None, None, None, None],
#  [None, 100, None, None, None, None, None, None, None],
#  [None, 100, None, None, None, None, None, None, None],
#  [None, 100, None, None, None, None, None, None, None],
#  [None, 100, None, None, None, None, None, None, None],
#  [None, 100, None, None, None, None, None, None, None],
#  [None, 100, None, None, None, None, None, None, None],
#  [None, 100, None, None, None, None, None, None, None]]

类似的事情应该起作用。

col = []  # Empty lists
val =[]

for i in range(0,rows,1):
    x = 0
    if i == 0:
        y += 70 
    else:
        y += 40
    rowc = []  # Empty row lists
    rowv = []
    for j in range(0,cols,1):
        x += 40
        v = IntVar()
        ent = Entry(root, width = 2, textvariable=v, font = 'Calibri 20', bd = 0, justify = CENTER)
        screen.create_window(x, y, window = ent)
        rowc.append(ent)   # Append to the rows
        rowv.append(v)
        x -= 5
    col.append(rowc)  # Append rows to the column lists
    val.append(rowv)