python - 二维数组中的 Tkinter 按钮绑定(bind)到相同的命令

标签 python tkinter

我正在制作一个简单的程序,它创建一个 tkinter.Button() 的 3x3 矩阵,当按下按钮时,它应该显示“Clicked”文本。 但结果似乎位于同一列上的按钮绑定(bind)到同一命令 - 该命令针对该列最后一行的按钮。

from tkinter import *

root = Tk()

text = [[None]*3]*3
buttons = [[None]*3]*3

def action(i, j):
    text[i][j].set('Clicked')

for i in range(3):
    for j in range(3):
        text[i][j] = StringVar()
        text[i][j].set('(%d, %d)' % (i,j))
        buttons[i][j] = Button(root, command = lambda i=i, j=j : action(i, j))
        buttons[i][j].config(textvariable = text[i][j], width = 9, height = 5)
        buttons[i][j].grid(row = i, column = j)

root.mainloop()

Image here

最佳答案

问题不在于您的命令,而在于您创建列表的方式。

当您乘以一个列表时,您实际上乘以对该单个列表的引用(请参阅 this question )。运行以下代码时您可以看到这一点:

text = [[None]*3]*3
print([id(x) for x in text])

因此,当您更改其中一个列表中的某个项目时,所有列表中的该项目都会发生更改。因此,您的列表看起来不像 [[1,2,3],[4,5,6],[7,8,9]],而是像 [[7, 8,9],[7,8,9],[7,8,9]]。然后,当您认为设置了 StringVar 编号 1 时,实际上您设置了 Stringvar 编号 7,因此按钮编号 7 发生了更改。

您可以使用列表理解而不是乘法来创建三个单独的列表。正如您在以下代码中看到的,这会生成三个单独的列表。

text = [[None]*3 for _ in range(3)]
print([id(x) for x in text])

关于python - 二维数组中的 Tkinter 按钮绑定(bind)到相同的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54400496/

相关文章:

python - 在全屏模式下显示单个图像(如 powerpoint)

unicode - Tkinter 条目接受 Unicode 文本?

python - 如何使用 Python 创建 "array"的多个 tkinter Scale 小部件?

python - split 错误 : Driver not found error, 为什么会这样说?

python - 从两行之间的 Pandas 数据框中提取行

python - Python中的MySQL,MATCH和注入(inject)攻击

python - 为 Python 2.x 和 3.x 编写模块

python - Django OAuth 工具包 "resource-owner password based"授权类型

python - 如何对 tkinter "scrolledtext"模块进行编码

python - 将按钮插入 python 上的 tkinter 列表框中?