tkinter for loop entry bind 连接到 sqlite3

tkinter for loop entry bind connected to sqlite3

我很难通过在 for 循环中创建的条目的绑定返回正确的 sqlite3 行

我知道,我不知道如何用语言来解释它所以这是一段代码

    if x == '':
        counter_kit_label = 10
        for kit in find_everything_from_db('*', 'kit'):
            kit_label = tk.Label(self.kit_listbox, text=f"{kit[0]}, {kit[1].capitalize()}, {kit[2].capitalize()},",
                                 bg='white')
            kit_label.place(x=5, y=counter_kit_label)
            kit_label.bind('<Enter>', lambda event: evidenzia(event))
            kit_label.bind('<Leave>', lambda event: non_evidenzia(event))
            kit_label.bind('<Button-1>', lambda: None)  # todo: creare event bind
            kit_entry_var = tk.StringVar()
            kit_entry = tk.Entry(self.kit_listbox, textvariable=kit_entry_var)
            kit_entry_var.set(f"{kit[4].capitalize()}")
            kit_entry.place(x=108, y=counter_kit_label)
            kit_entry.bind('<Return>', lambda event: self.modifica_luogo_kit(kit_entry.get(), kit, event))
            counter_kit_label += 30
    else:
        pass
def modifica_luogo_kit(self,luogo, kit, event=None):
    print(event.widget.get())
    print(kit)

当我运行脚本并在第一个tk.Entry上按'return-key'时,“print(event.widget.get())”的打印结果对应第一个从sqlite3选项卡的第一行预设的条目但是“print(kit)”对应于sqlite3选项卡的最新行

试试这个:

import tkinter as tk
from functools import partial

def modifica_luogo_kit(luogo, kit, event=None):
    print("Data in widget =", luogo.get())
    print("Entry number:", kit)

root = tk.Tk()

for kit in range(10):
    kit_entry = tk.Entry(root)
    kit_entry.pack()
    command = partial(modifica_luogo_kit, kit_entry, kit)
    kit_entry.bind("<Return>", command)

为了更容易解决问题,我删除了所有不必要的细节。我使用 functools.partial 创建了一个在按下“<Return>”时执行的命令。有关 functools.partial 的更多信息,请参阅 this