In Python 3.6.6, I would like to configure a tkinter Entry widget whenever another Entry widget is modified. Entry widgets don't have a callback function, but they do have validatecommand, which can work the same way if validate='focus'. However, the interpreter doesn't recognize the to-be-modified Entry widget within the validatecommand function. Here is my minimal example:
import tkinter as tk
class app:
def __init__(self,window):
self.root = window
self.tv1 = tk.DoubleVar(None,2)
self.tv2 = tk.DoubleVar(None,3)
self.tv3 = tk.DoubleVar(None,0)
self.entry1 = tk.Entry(self.root, textvariable=self.tv1,
validate='focus', validatecommand=self.valid)
self.entry2 = tk.Entry(self.root, textvariable=self.tv2,
validate='focus', validatecommand=self.valid)
self.label1 = tk.Label(self.root, textvariable=self.tv3)
self.entry1.pack()
self.entry2.pack()
self.label1.pack()
def valid(self):
self.tv3.set(self.tv1.get()*self.tv2.get())
self.label1.config(fg='red')
return True
main = tk.Tk()
win = app(main)
main.mainloop()
which returns
AttributeError: 'app' object has no attribute 'label1'
which is obviously not true.
The validate command is being called for the first time before the label is created.
You can solve this by creating the label before creating the entry widgets.