[Tutor] Need help
Peter Otten
__peter__ at web.de
Wed Sep 28 05:17:02 EDT 2016
niraj pandey wrote:
> Found the solution for this.
You can further simplifiy it with enumerate()
> entry_option = ['Flat_No','Mains Unit','DG Unit','Month']
> entry = {}
> label = {}
for r, item in enumerate(entry_option):
> lb = Label(bg = 'orange', text=item, relief=RIDGE,width=30)
> lb.grid(row=r,column=0)
> label[item] = lb
> e = Entry(relief=SUNKEN,width=30)
> e.grid(row=r,column=1)
> entry[item] = e
>
> But now how to pass these values as an argument for this function ?
>
> command=lambda: database.data(E1.get(), E2.get(), E3.get(), E4.get())
Well, you saved the Entry instances in a dict, so you can retrieve them:
command=lambda: database.data(*[entry[k].get() for k in entry_option])
If you use a list instead of or in addition to the dict
entries = []
for ...: # your loop from above
...
entries.append(e)
the lambda becomes
command=lambda: database.data(*[e.get() for e in entries])
If you have not come across it before: the * operator unpacks the list, so
args = ["a", "b"]
f(*args)
is equivalent to calling f with all items in the list,
f(args[0], args[1])
in the above example.
More information about the Tutor
mailing list