【问题标题】:Only last row of Entries is cleared when reset function is called调用 reset 函数时只清除最后一行 Entries
【发布时间】:2019-11-19 09:21:48
【问题描述】:

我正在练习 Python 的基本概念,并尝试在 Tkinter 的帮助下编写“数独”游戏。但是当我尝试使用重置功能清除所有条目字段时程序发生冲突,因为除了整个二维列表之外,只执行最后一行的条目。

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

【问题讨论】:

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


    【解决方案1】:

    构造 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)
    

    【讨论】:

    • 是的,我知道了,现在功能可以正常工作了,谢谢。
    猜你喜欢
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    相关资源
    最近更新 更多