Python Forum
AttributeError: 'NoneType' object has no attribute 'get' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: AttributeError: 'NoneType' object has no attribute 'get' (/thread-35848.html)



AttributeError: 'NoneType' object has no attribute 'get' - George87 - Dec-22-2021

Hi. I make my first steps on programming and i try to learn Python and Tkinter. I want to make a simple calc that has 2 entries and when i press the sum button make the sum and print the result on a label. The problem is that i get this error:
Quote:AttributeError: 'NoneType' object has no attribute 'get'
What i do wrong?

from tkinter import *

root = Tk()

def sum():
    result = Label (root, text=int(entry1.get()) + int(entry2.get())).pack()

entry1 = Entry(root).pack()
entry2 = Entry(root).pack()

sumbutton = Button (root, text="+", command=sum).pack()

root.mainloop()



RE: AttributeError: 'NoneType' object has no attribute 'get' - BashBedlam - Dec-22-2021

Believe it or not,entry1 = Entry(root).pack()is what's messing you up because it's the.pack()that's returning theNoneType. If you putentry1.pack()on a separate line, it will work.
from tkinter import *
def total () :
	Label(root,text=int(entry1.get()) + int(entry2.get())).pack()
root = Tk()
entry1 = Entry(root)
entry1.pack()
entry2 = Entry(root)
entry2.pack()
sumbutton = Button(root,text="+",command=total).pack()
root.mainloop()
Also, I wouldn't usesumif I were you because it's a python built-in.


RE: AttributeError: 'NoneType' object has no attribute 'get' - deanhystad - Dec-23-2021

Or you can do this:
import tkinter as tk

def add():
    result.set(entry1.get() + entry2.get())

root = tk.Tk()
entry1 = tk.IntVar(value=0)
entry2 = tk.IntVar(value=0)
result = tk.IntVar(value=0)
tk.Entry(root, textvariable=entry1).pack()
tk.Entry(root, textvariable=entry2).pack()
tk.Button(root, text="+", command=add).pack()
tk.Label(root, textvariable=result, width=5).pack()
root.mainloop()



RE: AttributeError: 'NoneType' object has no attribute 'get' - BashBedlam - Dec-23-2021

(Dec-23-2021, 02:39 AM)deanhystad Wrote: Or you can do this:
Nice. If you do this, you won't have to erase the zeros to enter your numbers.
entry1 = tk.IntVar(value='')
entry2 = tk.IntVar(value='')



RE: AttributeError: 'NoneType' object has no attribute 'get' - deanhystad - Dec-23-2021

Why would I want to do that? Then I am forced to enter a value for each entry.


RE: AttributeError: 'NoneType' object has no attribute 'get' - George87 - Dec-23-2021

Yes! It worked Dance Thanks a lot Smile