-1

I am making a simple GUI program in python using Tkinter. The problem is that when I try to retrieve the value from my Entry using a button, it comes back with an error():

print(ex1.get())
AttributeError: 'NoneType' object has no attribute 'get'

How to avoid this?

This is my code:

root = Tk()

root.minsize(300,300)
root.maxsize(300,300)


n = ttk.Notebook()

f1 = Frame(n,height=280,width=280)
f1.propagate(0)
lx1 = Label(f1,text="x",width=5).grid(row = 1,column=0) 
ex1 = Entry(f1,width = 10).grid(row = 1,column = 1)

ly1 = Label(f1,text="y",width=5).grid(row=3,column=0)
ey1 = Entry(f1,width = 10).grid(row=3,column = 1)

def value():
    print(ex1.get())


Bcreate = Button(f1,text="CREATE",command=value).grid(row = 10,column =  5)
n.add(f1,text="add point")

f2 = Frame(n)
n.add(f2,text="draw line")


n.pack()
Cœur
  • 37,241
  • 25
  • 195
  • 267
barty
  • 1
  • 1
  • I'm having a heck of a time just getting this to run anything at all. Is there any other code at all you can include? I imagine there must be an `import tkinter` or something like that. Also, pretty sure you'll need to `.pack()` in the various objects you've made, like `ex1`. – coralvanda May 11 '16 at 19:57

1 Answers1

1

Please read how to post a proper MCVE

Here is one for your problem:

from tkinter import Tk, Entry

root = Tk()
ent = Entry(root).pack()
print(ent)
print(ent.get())

This prints

None
Traceback (most recent call last):
  File "F:\Python\mypy\tem.py", line 6, in <module>
    print(ent.get())
AttributeError: 'NoneType' object has no attribute 'get'

At this point, it should be obvious that .pack() returns None. So instead bind the widget returned by Entry.

from tkinter import Tk, Entry

root = Tk()
ent = Entry(root)
ent.pack()
print(ent)
print(ent.get())

to get

.2425099144832
got  .
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52