如何 link python 在 for 循环中创建 tkinter 小部件?

How do I link python tkinter widgets created in a for loop?

我想用 for 循环创建 Button 和 Entry(state=disabled) 小部件。要创建的小部件的数量将是一个运行时参数。我想要的是每次单击按钮时,相应的条目将变为启用状态(state="normal")。我的代码中的问题是我单击的任何按钮,它只会影响最后一个条目小部件。有没有什么办法解决这一问题。?这是我的代码:

from tkinter import *

class practice:
    def __init__(self,root):
        for w in range(5):
            button=Button(root,text="submit",
                command=lambda:self.enabling(entry1))
            button.grid(row=w,column=0)
            entry1=Entry(root, state="disabled")
            entry1.grid(row=w,column=1)

    def enabling(self,entryy):
        entryy.config(state="normal")

root = Tk()
a = practice(root)
root.mainloop()

您的代码中存在一些问题 -

  1. 您应该保留 buttons 和您正在创建的条目并将它们保存在实例变量中,很可能最好将它们存储在列表中,然后 w 将是列表中每个 button/entry 的索引。

  2. 当你做 lambda: something(some_param) - some_param() 的函数值不会被替换,直到函数被实际调用,那时它正在处理entry1 的最新值,因此是问题所在。你不应该依赖于它,而应该使用 functools.partial() 并发送 Button/Entry 的索引来启用。

例子-

from tkinter import *
import functools

class practice:
    def __init__(self,root):
        self.button_list = []
        self.entry_list = []
        for w in range(5):
            button = Button(root,text="submit",command=functools.partial(self.enabling, idx=w))
            button.grid(row=w,column=0)
            self.button_list.append(button)
            entry1=Entry(root, state="disabled")
            entry1.grid(row=w,column=1)
            self.entry_list.append(entry1)

    def enabling(self,idx):
            self.entry_list[idx].config(state="normal")


root = Tk()
a = practice(root)

root.mainloop()

每当人们对使用 lambda 表达式而不是 def 语句创建的函数有疑问时,我建议使用 def 语句重写代码,直到它正常工作。这是对您的代码最简单的修复:它重新排序小部件创建并将每个条目绑定到一个新函数作为默认参数。

from tkinter import *

class practice:
    def __init__(self,root):
        for w in range(5):
            entry=Entry(root, state="disabled")
            button=Button(root,text="submit",
                command=lambda e=entry:self.enabling(e))
            button.grid(row=w,column=0)
            entry.grid(row=w,column=1)

    def enabling(self,entry):
        entry.config(state="normal")

root = Tk()
a = practice(root)
root.mainloop()