How can I use the same callback function to trace multiple variables?

前端 未结 1 1162
萌比男神i
萌比男神i 2021-01-27 14:40

I would like to display the value of several StringVar() with some formatting on Labels.

import tkinter as tk

keys = range(2) # 2 for simplicity

r         


        
相关标签:
1条回答
  • 2021-01-27 14:58

    You can send the key and the input to the function. This is a truncated version and a little different than your code but does what I think you want.

    import tkinter as tk
    from functools import partial
    
    def callback(key, var, *args):
        print "callback var =", key, var.get()
        ##myStrVars[key].set(var[-1])
    
    root = tk.Tk()
    
    for key in range(5):
        var = tk.StringVar()
        var.trace('w', partial(callback, key, var))
        tk.Entry(root, textvariable=var).pack()
    
    root.mainloop()
    
    0 讨论(0)
提交回复
热议问题