Nirevezs
Nirevezs

Reputation: 61

What is the easiest way to give values to entries in Tkinter, python?

My problem is I dont know how should I write get() method. So, I created a second empty list and when I append the oroginal list to the second one they get the values. That's obviously demand too much memory and it is too complicated (and ridiculous).

from tkinter import *

root = Tk()

Car = ['BMW', 'Mercedes', 'Ferrari', 'Ford', 'Toyota']
Attributes = ['Country', 'Modell', 'Color', 'Year', 'Price']

entriesList = []
entriesList2 = []

def appendToDict():
    for i in entriesList:
        entriesList2.append(i.get())       # must be simplier way
    print(entriesList2)

for i in range(len(Attributes)):
    myLab = Label(root, text=Car[i])
    myLab.grid(row=0, column=i+1)
    myLab2 = Label(root, text=Attributes[i])
    myLab2.grid(row=i+1, column=0)
    for j in range(len(Car)):
        myEntry = Entry(root)
        myEntry.grid(row=i+1, column=j+1)
        entriesList.append(myEntry)


MyButton = Button(root, text='Click me!', command=appendToDict).grid(row=7, column=3)

root.mainloop()

I tried this but it did not work in line 13:

 i =  i.get()

So, what's the easiest way?

Upvotes: 0

Views: 55

Answers (1)

astqx
astqx

Reputation: 2096

You could

  • Use map, to apply a function to all the items in an iterable.

    map_object = map(lambda ent:ent.get(), entriesList)
    entriesList2 = list(map_object)
    
  • Use list comprehension (as also mentioned in the comments by @CoolCloud)

    entriesList2 = [i.get() for i in entriesList]
    

Upvotes: 2

Related Questions