0

I am trying to make a record management system using tkinter but I am always getting this exception in tkinter callback error.

from tkinter import *

root2 = Tk()
root2.geometry('700x420')

def submit():
        firstname = e_firstname.get()
        lastname = e_lastname.get()
        age = e_age.get()
        phone_number = e_phone_number.get()

        print(firstname)
        print(lastname)
        print(age)
        print(phone_number)

Label(root2, text="Tilka Manjhi International Hospital Appointments", font=("Aharoni bold", 17)).grid(row=0, column=0, columnspan=2)
Label(root2, text="").grid(row=1)
Label(root2, text="First Name :" ,font=("ariel bold", 17)).grid(row=2, column=0)
e_firstname = Entry(root2, bd=5, width=40).grid(row=2, column=1)

Label(root2, text="").grid(row=3)
Label(root2, text="Last Name :" ,font=("ariel bold", 17)).grid(row=4, column=0)
e_lastname = Entry(root2, bd=5, width=40).grid(row=4, column=1)

Label(root2, text="").grid(row=5)
Label(root2, text="Age :" ,font=("ariel bold", 17)).grid(row=6, column=0)
e_age = Entry(root2, bd=5, width=40).grid(row=6, column=1)

Label(root2, text="").grid(row=7)
Label(root2, text="Phone Number :" ,font=("ariel bold", 17)).grid(row=8, column=0)
e_phone_number = Entry(root2, bd=5, width=40).grid(row=8, column=1)

Button(root2, text="Submit", command=submit).grid(row=9, column=1)


root2.mainloop()

I am always getting this error no matter how I change this code and still I dont get what I am doing wrong here.

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Hp\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__
    return self.func(*args)
  File "c:\Users\Hp\Desktop\python\project\patientbacklog.py", line 8, in submit
    firstname = efirstname.get()
AttributeError: 'NoneType' object has no attribute 'get'
  • 2
    This question has been answered here plenty of times, just google your exception. – Delrius Euphoria Jan 03 '21 at 14:38
  • The problem is that, `.grid()` does not return anything. You need a valid reference to your widget variable or store the user input in a dedicated variable (usually done with a `StringVar`) which can later be read via its `.get()` method. – albert Jan 03 '21 at 14:40

2 Answers2

0

You are getting the exception because you are creating an entry widget and you are griding it in the same line. That's not the right way , you should first create and assign a widget then you should grid it otherwise you will get the error because it can not recognize grid version of the widget

0

All the geometry managers (pack, grid and place) in tkinter return None. Since you have performed

e_firstname = Entry(root2, bd=5, width=40).grid(row=2, column=1)

Your variable e_firstname holds None as opposed to the Entry instance desired by you, hence when you perform any action on it it gives

AttributeError: 'NoneType' object has no attribute 'get'

Make the following changes and your program should work fine.

e_firstname = Entry(root2, bd=5, width=40)
e_firstname.grid(row=2, column=1)
astqx
  • 2,058
  • 1
  • 10
  • 21