[Tutor] Accessing an entry value input by the user

Pooja Bhalode poojabhalode11 at gmail.com
Sun Feb 12 15:01:05 EST 2017


Hi Alan,

Thank you so much for providing me your input on these things. I made all
the corrections to the code as per you suggested.

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

Code:

from Tkinter import *


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()

        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()

        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]
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.

But here, I am not able to store the value associated with each entry boxes
in the created list.
Can you please let me know where I am going wrong?
Thank you Alan.

Pooja

On Sat, Feb 11, 2017 at 8:03 PM, Alan Gauld via Tutor <tutor at python.org>
wrote:

> On 11/02/17 18:59, Pooja Bhalode wrote:
> > Hi Alan,
> >
> > I had done what you suggested here, I also tried creating another file
> for
> > that snipet of the code to see if that section works. The other file
> works,
> > but I am not able to figure out why the original one doesn't work.
>
> Too late at night for a detailed analysis but your code
> should be restructured its ghetting very messy and hard
> to see whats going on. More on that another time.
> Meanwhile some immediate thoughts...
>
> > from Tkinter import *
> > import datetime
> > import tkMessageBox
> > from tkFileDialog import *
> > from tkMessageBox import *
>
> If you do this there's no point in importing tkmessagebox earlier.
>
> > root = Tk()
> > root.title("Design of Experiments with Parameter Estimation")
> > root.geometry("1000x1000")
> >
> > statusvar = StringVar()
> > statusvar = "Status Bar"
>
> You create the stringvar but then throw it waay by overwriting it with a
> string.
>
> Maybe you meant to do:
>
> statusvar = StringVar()
> statusvar.set("Status Bar")
>
> ???
>
>
> > var1 = IntVar()
> > var2 = IntVar()
> > var3 = IntVar()
> > var4 = IntVar()
> >
> > varreac = IntVar()
> > varint = IntVar()
> > varpro = IntVar()
> >
> > varconc = IntVar()
> > vartemp = IntVar()
> > entrynumberspeciesvar = IntVar()
>
> You can only use a stringvar with an Entry because
> the Entry only holds text, not integers. You will
> need to do the conversions yourself when you set/get
> the values.
>
> > def DesignPoint():
> >     print "Inside Design Point"
> >     rootdesign=Tk()
>
> You are still creating multiple roots, that is
> really bad practice and almost sure to create
> problems later. Define the function as:
>
> def DesignPoint(root):...
>
> and call it as
>
> DesignPoint(root)
>
> >     rootdesign.title("Design Point Suggestions")
> >     rootdesign.geometry("700x400")
> >     frame1 = Frame(rootdesign)
> >     frame1.grid(row=0, column=0)
> >
>
> If you want a second window you should be
> using a Toplevel widget not Frame here.
>
>
> >     label1 = Label(frame1, text="1. Responses to include: ")
> >     label1.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)
> >
> >     # print entrynumberspeciesvar.get()
> >     checkreac = Checkbutton(frame1, text = "Reactant species", variable =
> > varreac)
> >     checkreac.grid(row = 1, column = 1, sticky = W)
> >     checkreac.select()
> >     Checkbutton(frame1, text = "Intermediate species", variable =
> > varint).grid(row = 2, column = 1, sticky = W)
> >     Checkbutton(frame1, text = "Product species", variable =
> > varpro).grid(row = 3, column = 1, sticky = W)
> >
> >     def Default():
> >         print "Inside default"
> >
> >         var1.set(0)
> >         var2.set(0)
> >         var3.set(0)
> >         var4.set(1)
>
> This function really should be defined outside
> the DesignPoint one. It will be much easier to maintain
> if you separate them out.
>
> >     Checkbutton(frame1, text = "Vertices", variable=var1, onvalue=1,
> > offvalue=0).grid(row=1, column = 2, sticky=W)
> >     Checkbutton(frame1, text = "Edges", variable=var2).grid(row=2,
> column =
> > 2, sticky=W)
> >     Checkbutton(frame1, text = "Faces", variable=var3).grid(row=3,
> column =
> > 2, sticky=W)
> >     check = Checkbutton(frame1, text = "Center", variable=var4)
> >
> >     check.grid(row=4, column = 2, sticky=W)
> >     check.select()
> >
> >     Label(frame1, text="2. Variables to be adjusted:").grid(row=5,
> > column=0,sticky=W)
> >     Checkbutton(frame1, text = "Concentration", variable=varconc,
> > onvalue=1, offvalue=0).grid(row=5, column = 1, sticky=W)
> >     Checkbutton(frame1, text = "Temperature",
> variable=vartemp).grid(row=6,
> > column = 1, sticky=W)
> >
> >     def InsertRange():
> >         print "Inside InsertRange"
> >         # entrynumberspeciesvar.set(2)
> >         print entrynumberspeciesvar.get()
> >         for i in range(entrynumberspeciesvar.get()):
> >             textvar = StringVar()
> >             print i
> >             textvar = "\t Conc"+str(i)
>
> Again you have deleted your StringVar object by overwriting it.
> You need to set the value with the set() method. OTOH you
> never use the StringVar so maybe you just need to delete
> that line.
>
> >             Label(frame1, text=textvar).grid(row=(9+i),
> column=0,sticky=W)
> >             conclowerentry = Entry(frame1)
> >             conclowerentry.grid(row= (9+i), column = 1, sticky = W)
> >             concupperentry = Entry(frame1)
> >             concupperentry.grid(row= (9+i), column = 2, sticky = W)
> >
>
> Same goes here. You should have very good reasons to define event
> handlers inside the functions that build your UIs. It usually just makes
> the code more complex with no benefit.
>
> >     Label(frame1, text="3. Range of formulation:").grid(row=7,
> > column=0,sticky=W)
> >     Button(frame1, text = "Insert Range", command =
> InsertRange()).grid(row
> > = 7, column = 3, sticky=W)
> >     Label(frame1, text="Lower Limit").grid(row=7, column=1,sticky=W)
> >     Label(frame1, text="Upper Limit").grid(row=7, column=2,sticky=W)
> >
> >     Label(frame1, text="\t Temperature").grid(row=8, column=0,sticky=W)
> >     templowerentry = Entry(frame1, text="0.0")
> >     templowerentry.grid(row= 8, column = 1, sticky = W)
> >     tempupperentry = Entry(frame1)
> >     tempupperentry.grid(row= 8, column = 2, sticky = W)
> >
> >     rootdesign.mainloop()
> >
> >
> > ## Secondary menu bar:
> > menusec = Frame(root, bg="white")
> > butt1 = Button(menusec, text="Part One", command=DesignPoint)
> > butt1.pack(side=LEFT, padx=1)
> > menusec.pack(side=TOP, fill=X)
> >
> >
> > ### --- Status bar ---- ####
> > Status = Label(root, text = statusvar, bd=1, relief=SUNKEN, anchor=W)
> > Status.pack(side=BOTTOM, fill=X)
> >
> > text = Text(root, width=1000, height = 400)
> > text.pack(side=BOTTOM)
> >
> >
> > root.mainloop()
> >
> > I have removed other parts of the code and included only the ones related
> > to the entry box and the work that I need to do.
> > I also tried doing it in another file as mentioned before. That works the
> > exact way I want.
> >
> > Code:
> > from Tkinter import *
> >
> > rootdesign=Tk()
> > rootdesign.title("Design Point Suggestions")
> > rootdesign.geometry("650x400")
> > frame1 = Frame(rootdesign)
> > frame1.grid(row=0, column=0)
> > entrynumberspeciesvar = IntVar()
> > ## Inserting Checkboxes:
> > label1 = Label(frame1, text="1. Responses to include: ")
> > label1.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)
> >
> > def Print():
> >     print entrynumberspeciesvar.get()
> >     for i in range(entrynumberspeciesvar.get()):
> >         print i
> >         textvar = "\t Conc"+str(i+1)
> >         Label(frame1, text=textvar).grid(row=(9+i), column=0,sticky=W)
> >         conclowerentry = Entry(frame1)
> >         conclowerentry.grid(row= (9+i), column = 1, sticky = W)
> >         concupperentry = Entry(frame1)
> >         concupperentry.grid(row= (9+i), column = 2, sticky = W)
> >
> > Button(frame1, text = "Click", command = Print).grid(row = 2, column = 2,
> > sticky = W)
> > Label(frame1, text="\t Temperature").grid(row=8, column=0,sticky=W)
> > templowerentry = Entry(frame1, text="0.0")
> > templowerentry.grid(row= 8, column = 1, sticky = W)
> > tempupperentry = Entry(frame1)
> > tempupperentry.grid(row= 8, column = 2, sticky = W)
> > rootdesign.mainloop()
> >
> >
> > Please let me know where I am going wrong. Thank you so much.
> > Pooja
> >
> > On Sat, Feb 11, 2017 at 12:52 PM, Alan Gauld via Tutor <tutor at python.org
> >
> > wrote:
> >
> >> On 11/02/17 15:28, Pooja Bhalode wrote:
> >>
> >>> I am trying to create a label and an entry widget. I am not able to
> >>> understand as to how to access the value input by the user in the entry
> >>> widget.
> >>>
> >>> Label(frame1, text = "Number of species:").grid(row=0, column = 1,
> >> sticky=W)
> >>> entrynumberspecies = Entry(frame1)
> >>> entrynumberspecies.grid(row=0, column = 2, sticky=W)
> >>>
> >>> print entrynumberspecies.get()
> >>
> >> You just accessed it, via the get() method. Did that not work?
> >>
> >>> How can I make the entrynumberspecies store the value in once the user
> >>> inputs it and then use that value for later part of my code?
> >>
> >> You can do it the way you did above using the get() method.
> >>
> >> But you can also do what you did for the checkboxes - use
> >> a StringVar and attach it to the Entry widget textvariable
> >> attribute. That way the variable will reflect whats in
> >> the Entry automatically and if you update the variable
> >> it will update the Entry. (Personally I prefer to use
> >> get() in most cases but many use the StringVar technique.)
> >> See the example at the bottom...
> >>
> >>> or print it for that matter.
> >>
> >> You can print it as you did above or you can store it in
> >> a variable and then print it or you can print the StringVar:
> >>
> >> print myEntry.get()
> >>
> >> myVar = MyEntry.get()
> >> print myVar
> >>
> >> entryVar = StringVar()
> >> myEntry = Entry(.....textvariable=entryVar)
> >> print entryVar.get()
> >>
> >> Here is a minimal example:
> >>
> >> ################
> >> from Tkinter import *
> >>
> >> def show():
> >>     print "entry says: " + e.get()
> >>     print "Variable holds: " + v.get()
> >>
> >> top = Tk()
> >> v = StringVar()
> >> e = Entry(top,textvariable=v)
> >> e.pack()
> >> Button(top,text="Set foobar", command=lambda : v.set("foobar")).pack()
> >> Button(top,text="Show me", command=show).pack()
> >>
> >> top.mainloop()
> >> #################
> >>
> >>
> >> --
> >> 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
> >>
> >>
> >> _______________________________________________
> >> Tutor maillist  -  Tutor at python.org
> >> To unsubscribe or change subscription options:
> >> https://mail.python.org/mailman/listinfo/tutor
> >>
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
>
>
> --
> 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list