I'm working with Tkinter in Python, and I created multiple TextWidgets. I put these Objects in a list called output. Each text widget has the property text, which can be changed with the methods .delete(index1,index2) and insert(index, chars).
Now, I'd like to apply the insert function. From another fuction kg_to_pounds_ounces_grams I get a list of values for the Text Widgets:
def kg_to_pounds_ounces_grams(kilogram):
pound = kilogram * 2.20462
oounce = kilogram * 35.274
gram = kilogram * 1000
return [pound, oounce, gram]
How can I apply insert to Output so that pound goes into TextWidget1, ounce into TextWidget2 and gram into TextWidget3, and call the function kg_to_pounds_ounces_grams only one time in a one line expression? Same goes for delte - also applied to all in one line?
Edit: I managed to do both in three lines:
for textWidget,weight in zip(output, kg_to_pounds_ounces_grams(kg) ):
textWidget.delete(1.0, END)
textWidget.insert(END,weight)
But it still bothers me - is there no elegant two-line solution?
For better understanding, I put the full code here:
from tkinter import *
window = Tk()
def kg_to_pounds_ounces_grams(kilogram):
pound = kilogram * 2.20462
ounce = kilogram * 35.274
gram = kilogram * 1000
return [pound, ounce, gram]
def convert_button_pressed():
try:
kg = float(e1_text.get())
except:
kg = float("NaN")
map(lambda x: x.delete(1.0, END), output)
# Missing Code goes here!
l1 = Label(window, text="Kg")
l1.grid(row=0, column=0)
e1_text = StringVar()
e1 = Entry(window, textvariable=e1_text)
e1.grid(row=0, column=1)
b1 = Button(window, text="Convert", command=convert_button_pressed)
b1.grid(row=0, column=2)
t1 = Text(window, height=1, width=20)
t1.grid(row=1, column=0)
t2 = Text(window, height=1, width=20)
t2.grid(row=1, column=1)
t3 = Text(window, height=1, width=20)
t3.grid(row=1, column=2)
output = [t1, t2, t3]
window.mainloop()
map(lambda x: x.insert(END,out) for out in kg_to_pounds_ounces_grams(kg), output)