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
root = tk.Tk()
myVars = {key: tk.StringVar() for key in range(5)}
myStrVars = {key: tk.StringVar() for key in range(5)}
def callback0(*args):
blah = '{0:.3f}'.format(float(myVars[0].get()))
myStrVars[0].set(blah)
def callback1(*args):
blah = '{0:.3f}'.format(float(myVars[1].get()))
myStrVars[1].set(blah)
#etc...
myCallbacks = {0: callback0,
1: callback1}
#etc...
for key in keys:
myVars[key].trace('w', myCallbacks[key])
tk.Entry(root, textvariable=myVars[key]).pack()
label = tk.Label(root, textvariable=myStrVars[key]).pack()
root.mainloop()
Is there a way of writing a callback function so that I don't have to write one for each variable that I want to trace?