user3583227
user3583227

Reputation: 41

Entry().get() doesn't work in Python 3.4

Despite all attempts, I cannot seem to get Entry().get() to assign a string in an Entry window. Here's a code snippet:

    Itx_bcn_ent = Entry(win).grid(row=1,column=1)

I define a button to call a function:

    btn = Button(win,text="Run",command=getnums).grid(row=5,column=3,padx=100)

Here's the function: def getnums(): Itx_bcn = Itx_bcn_ent.get()

When I run the script, I get the following error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
    return self.func(*args)
  File "C:\Python34\voltage_substate_GUI.py", line 7, in getnums
    Itx_bcn = Itx_bcn_ent.get()
AttributeError: 'NoneType' object has no attribute 'get'

I've seen the construct to use a Class StringVar() and the option "textvariable=" with the Entry() object, however doing this seems overly complicated as it just creates an additional set of variables between what's in the Entry window and the variable I am trying to assign.

Any thoughts on this?

Upvotes: 2

Views: 958

Answers (1)

Adam Smith
Adam Smith

Reputation: 54203

Entry.grid() returns None, so when you do something = Entry(root).grid(), you're getting something=None.

This isn't a problem until you try to use that thing! That's why you're getting a 'NoneType' object has no attribute 'get' error.

Itx_bcn_ent = Entry(win)
Itx_bcn_ent.grid(row=1,column=1)

Now your button works :), though you have the same problem with your btn = line. I've yet to have to reuse that assignment, so maybe just drop it?

Button(win,text="Run",command=getnums).grid(row=5,column=3,padx=100)

Upvotes: 3

Related Questions