0

So I am trying to make a simple calculator program using Tkinter and python. I have some general code down for addition and subtraction but am getting this error. Please advise, the code is written below.

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python32\lib\tkinter\__init__.py", line 1399, in __call__
    return self.func(*args)
  File "C:\Users\**\workspace\calcApp\calcApp\guiCalc.py", line 21, in numClick
    input = int(entry.get())
AttributeError: 'NoneType' object has no attribute 'get'

guiCalc.py:

from tkinter import *

class Calc:
 def init():

  root = Tk()
  root.wm_title("Calculator")

  input = 0
  varIn = StringVar()
  varIn = ""
  labelText = StringVar()
  ans = ""
  ans2 = ""

  entry = Entry(root).grid()

  def numClick():
   input = int(entry.get()) 
   entry.delete(0, END)   


  def equalClick():
   if(entry.get()=="+"):
    ans = input + int(entry.get())
    label.configure(text=ans)
   if(entry.get()=="-"):
    ans2 = input-int(entry.get())
    label.configure(text = ans2)

  Button(root, text="+", command=numClick).grid()    
  Button(root, text="-", command=numClick).grid()
  Button(root, text="=", command =equalClick).grid()

  label = Label(root, text="")
  label.grid()
  root.mainloop()



Calc.init()
Silas Ray
  • 25,682
  • 5
  • 48
  • 63
user3290306
  • 11
  • 1
  • 2
  • 4

2 Answers2

3
entry = Entry(root).grid()

entry is None here, because grid doesn't return anything. Perhaps you meant to do:

entry = Entry(root)
entry.grid()
Kevin
  • 74,910
  • 12
  • 133
  • 166
0

I am kind of new, yes, but let me share what I've learned.

Appending .grid() in the widget´s creation line works flawlessly most of the times for layout purpose, but is not a good practice. The correct thing is to make a new line like Kevin said.

Will
  • 24,082
  • 14
  • 97
  • 108
Ito
  • 1
  • 4