Python Forum
AttributeError: 'NoneType' object has no attribute 'get'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'NoneType' object has no attribute 'get'
#1
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()
Reply
#2
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.
George87 and Gribouillis like this post

Attached Files

Thumbnail(s)
   
Reply
#3
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()
BashBedlam and George87 like this post
Reply
#4
(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='')
Reply
#5
Why would I want to do that? Then I am forced to enter a value for each entry.
Reply
#6
Yes! It worked Dance Thanks a lot Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  'NoneType' object has no attribute 'get' zunebuggy 8 1,243 Oct-13-2023, 06:39 PM
Last Post: zunebuggy
  tkinter AttributeError: 'GUI' object has no attribute pfdjhfuys 3 1,452 May-18-2023, 03:30 PM
Last Post: pfdjhfuys
  [Kivy] Windows 10: AttributeError: 'WM_PenProvider' object has no attribute 'hwnd' mikepy 1 2,246 Feb-20-2023, 09:26 PM
Last Post: deanhystad
  [Tkinter] Can't update label in new tk window, object has no attribute tompranks 3 3,467 Aug-30-2022, 08:44 AM
Last Post: tompranks
  [PyQt] AttributeError: 'NoneType' object has no attribute 'text' speedev 9 11,237 Sep-25-2021, 06:14 PM
Last Post: Axel_Erfurt
  [Tkinter] AttributeError: '' object has no attribute 'tk' Maryan 2 14,446 Oct-29-2020, 11:57 PM
Last Post: Maryan
  [Tkinter] AttributeError: 'tuple' object has no attribute 'replace' linuxhacker 7 6,758 Aug-08-2020, 12:47 AM
Last Post: linuxhacker
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 6,191 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' edphilpot 5 9,109 Dec-20-2019, 09:52 PM
Last Post: joe_momma
  [Tkinter] AttributeError: 'App' object has no attribute 'set_text' Sahil1313 6 11,993 Jun-17-2018, 05:01 AM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020