0
from tkinter import *

root=Tk()
root.title("TKINTER WINDOW")
root.geometry("300x300")
root.resizable(width="false",height="false")

entry=Entry(root).place(x=50,y=0)

def get_name():
    print(entry.get())

label=Label(root,text="Name: ").place(x=0,y=0)

btn=Button(root,text="click me",command= lambda : get_name()).place(x=100,y=100)

root.mainloop()

I'm trying to get the value of entry but getting this error.I'm new to python...stackoverflow keeps telling me to add more lines or it won't post my question but i have nothing else to say.

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Manish Gusain\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/Manish Gusain/PycharmProjects/untitled/practice.py", line 15, in <lambda>
    btn=Button(root,text="click me",command= lambda : get_name()).place(x=100,y=100)
  File "C:/Users/Manish Gusain/PycharmProjects/untitled/practice.py", line 11, in get_name
    print(entry.get())
AttributeError: 'NoneType' object has no attribute 'get'

Process finished with exit code 0```


Manish Gusain
  • 35
  • 1
  • 7
  • 1
    `entry` is the returned value of `place` method which is `None`.you should write `entry=Entry(root)` then `entry.place(x=50,y=0)` – Kenly Oct 23 '19 at 12:42

1 Answers1

0

Problem is that entry is the name for Entry(root).place(x=50, y=0) which is None. (same for the name label and btn). So as mentioned in the comment you need to name entry the object Entry(root).

As you are using a lambda you can pass the object entry to the function get_name by lambda: get_name(entry). So this will work

from tkinter import Tk, Entry, Button, Label

def get_name(my_entry):
    print(my_entry.get())

root = Tk()
root.title("TKINTER WINDOW")
root.geometry("300x300")
root.resizable(width="false", height="false")

label = Label(root, text="Name: ")
label.place(x=0, y=0)

entry = Entry(root)
entry.place(x=50, y=0)

btn = Button(root, text="click me",
    command=lambda: get_name(entry))
btn.place(x=100, y=100)

root.mainloop()

However, rather than passing the entry object to the function via a lambda, I prefer to create a class so I can share the namespace.

from tkinter import Tk, Entry, Button, Label

class MyEntry:

    def __init__(self):

        self.root = Tk()
        self.root.title("TKINTER WINDOW")
        self.root.geometry("300x300")
        self.root.resizable(width="false", height="false")

        self.label = Label(self.root, text="Name: ")
        self.label.place(x=0, y=0)

        self.entry = Entry(self.root)
        self.entry.place(x=50, y=0)

        self.btn = Button(self.root, text="click me",
             command=self.get_name)
        self.btn.place(x=100, y=100)

        self.root.mainloop()

    def get_name(self):
        print(self.entry.get())

if __name__ == '__main__':
    MyEntry()

Some tips:

  • Do not do import *
  • Do use the if __name__ == '__main__' construct
Bruno Vermeulen
  • 2,970
  • 2
  • 15
  • 29