[Tutor] Accessing an entry value input by the user

Pooja Bhalode poojabhalode11 at gmail.com
Sat Feb 11 13:59:08 EST 2017


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.
The variable here is entrynumberspeciesvar

Code:
from Tkinter import *
import datetime
import tkMessageBox
from tkFileDialog import *
from tkMessageBox import *
root = Tk()
root.title("Design of Experiments with Parameter Estimation")
root.geometry("1000x1000")

statusvar = StringVar()
statusvar = "Status Bar"

var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var4 = IntVar()

varreac = IntVar()
varint = IntVar()
varpro = IntVar()

varconc = IntVar()
vartemp = IntVar()
entrynumberspeciesvar = IntVar()

def DesignPoint():
    print "Inside Design Point"
    rootdesign=Tk()
    rootdesign.title("Design Point Suggestions")
    rootdesign.geometry("700x400")
    frame1 = Frame(rootdesign)
    frame1.grid(row=0, column=0)

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


    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
>


More information about the Tutor mailing list