0

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()
3
  • I'm sure, it's possible by using map, lambda and for in the right way. My last (failed) try looks like this: map(lambda x: x.insert(END,out) for out in kg_to_pounds_ounces_grams(kg), output) Commented Jan 26, 2018 at 9:58
  • 2
    why is it important to have a two-line solution? Trying to condense the code down will only serve to make it harder to read and harder to debug. Commented Jan 26, 2018 at 19:58
  • Are you simply wanting to insert the three values returned by the function? What's wrong with simply joining the results into a string and inserting the string? Commented Jan 26, 2018 at 20:00

1 Answer 1

1

Similar to your delete line you can do the insert in a single line... but for readability's sake I wouldn't recommend these one-liners.

It should be noted that using lambda in conjuction with map is a bit silly, a list comprehension would be cleaner:

[x.delete(1.0, END) for xin output]

And for the insert:

[x.insert(END, w) for x, w in zip(output, kg_to_pound_ounces_grams(kg))]

I suppose you could even combine them, but once again I don't recommend either of these.

[(x.delete(1.0, END), x.insert(END, w)) for x, w in zip(output, kg_to_pound_ounces_grams(kg))]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.