[Python] How to get a reference to traced variable in tkinter

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I have a tkinter application with variable number of entries and I want to detect changes to the contents and specifically, which entry has been changed

    import tkinter as tk
    from tkinter import ttk

    DOGS = 3

    def main() -> None:
    root = tk.Tk()
    root.title('')
    root.geometry('400x400')

    dogs = {}
    entries = {}

    for row in range(DOGS):
    dogs[row] = tk.StringVar()
    dogs[row].trace_add('write', dog_changed)
    entries[row] = ttk.Entry(root, textvariable=dogs[row])
    entries[row].grid(row=row, column=0)

    root.mainloop()

    def dog_changed(*args):
    print(args)

    if __name__ == '__main__':
    main()


    I can see in args that I have the name of the StringVar as a string (e.g."PY_VAR0"), but is there a more elegant way of getting a reference to which entry has been changed rather than taking a substr here.

    I have tried this lambda function, but of course it only show the current value of row

    dogs[row].trace_add('write', lambda *args: dog_changed(row, *args))

    Continue reading...

Compartilhe esta Página