Is it possible to have 2 entry widgets that are interactive, I'm thinking about conversions here, so for example Miles to Kilo-meters & vice-versa; as I enter data into one widget, it is automatically updated in the other widget, regardless of which one I start with.
Below is my starting point, but I need the entry widgets to be sensitive to "Key" and call a respective function?
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("Conversions")
root.geometry("500x200")
ent_pound = Entry(root, width=20)
ent_pound.grid(row=0,column=0)
ent_kilo = Entry(root, width=20)
ent_kilo.grid(row=1,column=0)
value_pound=eval(ent_pound.get())
ent_kilo.delete(0,END)
ent_kilo.insert(0,repr(value*0.453592))
value_kilo=eval(ent_kilo.get())
ent_pound.delete(0,END)
ent_pound.insert(0,repr(value/0.453592))
`
root.mainloop()
The best way to do this is using Tkinter variables. You can create a
DoubleVarfor each entry, then add a trace to call a function on write. The variables are then bound to the entries with thetextvariableattribute.You can then use the
getandsetmethods of the variables to do the conversion. The conversion is wrapped in a try/except to ignore the error fromgetif the entry is empty.If you don't like how
DoubleVarprefills the entry, you could useStringVarinstead and convert the input