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

Sample program:

import tkinter as tk
master = tk.Tk()
tk.Label(master, text="First Number").grid(row=0, column=0)
tk.Label(master, text="Second Number").grid(row=1, column=0)
tk.Label(master, text="Value").grid(row=2, column=0)
# Display entry fields
e1 = tk.Entry(master).grid(row=0, column=1) 
e2 = tk.Entry(master).grid(row=1, column=1)
def display():
            a=int(e1.get())
            b=int(e2.get())
            tot=a=b
            tk.Label(master, text=str(tot)).grid(row=2, column=1)       
# end of display function
# button to display all the calculated total 
button1=tk.Button(master, text="Submit", bg="green", command=display)
button1.grid(row=6, column=1)
master.mainloop()

    Output:
    Error:
    1. Exception in Tkinter callback
      Traceback (most recent call last):
        File "C:\Users\KUMAR\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883,
       in __call__
          return self.func(*args)
        File "C:/Users/KUMAR/AppData/Local/Programs/Python/Python38-32/myprgm/rough.py", line 10,
       in display
          a=int(e1.get())
      AttributeError: 'NoneType' object has no attribute 'get'.

    Problem:
    •  None Type means, You got no value. It means an assignment or function call up failed.
    Solution:
    •  If the entry field contains some calculation, then entry field declaration and place value must be separate.

    e1 = tk.Entry(master)
    e2 = tk.Entry(master)
    e1.grid(row=0, column=1) 
    e2.grid(row=1, column=1) 

    Share:

    1 comment:

    Recent Posts

    Service Support

    Need our help to Learn or Post New Concepts Contact me