问题
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()
回答1:
entry = Entry(root).grid()
entry
is None
here, because grid
doesn't return anything. Perhaps you meant to do:
entry = Entry(root)
entry.grid()
回答2:
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.
来源:https://stackoverflow.com/questions/21913394/getting-traceback-and-attributeerror-nonetype-object-has-no-attribute-get