[Tutor] Accessing an entry value input by the user

Alan Gauld alan.gauld at yahoo.co.uk
Sun Feb 12 21:05:14 EST 2017


On 12/02/17 20:01, Pooja Bhalode wrote:

> Thank you so much. I got stuck in another section:

It would be helpful if you gave more details of exactly how you get stuck.
What is happening(or not) that puzzles you.
Otherwise we just have to guess at what you mean.

Anyway, here goes...

> root=Tk()
> root.title("Design Point Suggestions")
> root.geometry("650x400")
> frame1 = Frame(root)
> frame1.grid(row=0, column=0)
> entrynumberspeciesvar = IntVar()
> Label(frame1, text="1. Responses to include: ").grid(row=0,
> column=0,sticky=W )
> Label(frame1, text = "Number of species:").grid(row=0, column = 1, sticky=W)
> entrynumberspecies = Entry(frame1, textvariable = entrynumberspeciesvar)
> entrynumberspecies.grid(row=0, column = 2, sticky=W)
> 
> conclowerentry = []
> concupperentry = []
> 
> def Print():
>     print entrynumberspeciesvar.get()
>     for i in range(entrynumberspeciesvar.get()):
>         conclowertemp = StringVar()
>         concuppertemp = StringVar()

Once again you are defining variables inside the function which
means they disappear when the function ends.

If you want to access what these hold you need to have them
outside the function.

>         textvar = "\t Conc"+str(i+1)
>         Label(frame1, text=textvar).grid(row=(9+i), column=0,sticky=W)
>         Entry(frame1, textvariable = conclowertemp).grid(row= (9+i), column
> = 1, sticky = W)
>         Entry(frame1, textvariable = concuppertemp).grid(row= (9+i), column
> = 2, sticky = W)
> 
>         if len(concuppertemp.get()) != 0 and len(conclowertemp.get()) != 0:
>             concupperentry.append(int(concuppertemp.get()))
>             conclowerentry.append(int(conclowertemp.get()))
> 
>         def Submit():
>             print "Submitted. "
>             print conclowerentry.get()
>             print concupperentry.get()

Again this function could(should?) be outside the function
as a global level function. By defining it inside the function
you add extra complexity to the Print() code and break up
its flow making it harder to see what is going on.

You gain nothing by having it inside the Print() since
the variables it accesses are already declared outside
Print and are thus available for Submit to access.

> 
>         Button(frame1, text="Submit", command = Submit).grid(row = 2,
> column = 1, sticky = W)
> 
> Button(frame1, text = "Click", command = Print).grid(row = 2, column = 2,
> sticky = W)
> root.mainloop()
> 
> print conclowerentry
> print concupperentry
> 
> This creates a window as given below:
> [image: Inline image 1]

This is a text based mailing list so images usually
get stripped out by the server.


> where the user inputs the number and the corresponding number of entry
> boxes and labels get created.
> Submit button is used for storing in the values in a list and then
> printing.

Submit is not storing anything it just prints.
The only storage is in this segment of Print()

>         if len(concuppertemp.get()) != 0 and len(conclowertemp.get())
!= 0:
>             concupperentry.append(int(concuppertemp.get()))
>             conclowerentry.append(int(conclowertemp.get()))
>

And since that is called immediately after the creation of the Entry I
doubt that anything gets stored. You need to put this code inside the
Submit function.
> 
> But here, I am not able to store the value associated with each entry boxes
> in the created list.

See above.

Move the Submit function outside Print() and put the
storage lines above inside Submit() and see how that goes.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list