0

I am trying to return an answer from simple addition to an entry box in Python 3.4 but keep getting the following error. Exception in Tkinter callback

Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "H:\guitest.py", line 12, in calculate
    enter3.configure(text=answer)
AttributeError: 'NoneType' object has no attribute 'configure'


#Imports The Default Graphical Library
import tkinter
from tkinter import *

#Addition Function
def calculate():
    """ take the two numbers entered and process them """
    firstnum=int(number1.get())

    secondnum=int(number2.get())
    answer=firstnum+secondnum
    enter3.configure(text=answer)
    return


#Creates Graphical Window
window = Tk()
# Assigns a Name To the Graphical Window
window.title("MY GUI")
# Set the Graphical Window Size
window.geometry("500x200")

number1=StringVar()
number2=StringVar()

label1 = Label(text='1st Number').place(x=50,y=30)
label2 = Label(text='2nd Number').place(x=150,y=30)
label3 = Label(text='ANSWER').place(x=100,y=80)
enter1 =Entry(width=10,textvariable=number1).place(x=50,y=50)
enter2 =Entry(width=10,textvariable=number2).place(x=150,y=50)
enter3 =Entry(width=10).place(x=100,y=100)



#Creates Button
w = Button(text='ADD',bd=10,command=calculate).place(x=100,y=150)

#Executes the above Code to Create the Graphical Window
window.mainloop()

1 Answers1

0

There are at least three problems here.

  1. The variables that are supposed to be holding UI elements (e.g. label1, entry3) are not, in fact, holding them. They are set to None. The place method is not returning the object being placed. It's just doing the placing. So you'd need to change your initialization strategy to something like:

    enter3 = Entry(width=10)
    enter3.place(x=100,y=100)
    

    If you argued that non-fluid style is less graceful, I'd agree...but apparently place does not return the object, so you can't do en passant creation and placement.

  2. entry3.configure(text=answer) is not going to work, because answer is not a string. It's an integer. You need entry3.configure(text=str(answer)). At minimum.

  3. I lied. That is not going to work either, because configure is not how Entry widget text is usually set. Try instead:

    entry3.delete(0, END) # delete whatever's there already
    entry3.insert(0, str(answer))
    entry3.update() # make sure update is flushed / immediately visible
    

    See e.g. this tutorial for more on Entry objects. And while explicit widget.update() calls may look clunky, trust me--they come in super-handy when things don't update as you think they should.

  4. I'm not sure why you're using a hybrid strategy. For inputs, you use StringVar objects, but then you shift to a raw update for the answer widget. Not that it won't work...but it might be easier just to set number3 = StringVar() and attach it as the textvariable of entry3, then number3.set(str(answer)) when you want to update it. Pick your poison.

Even if you make these changes, you'll have some work to do to have a "good" Tk app, but these will at least make the updates work as they should, and get you on your way.

Jonathan Eunice
  • 21,653
  • 6
  • 75
  • 77