Python Forum
'NoneType' object has no attribute 'get'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'NoneType' object has no attribute 'get'
#4
Post entire error message, including trace.

This is your problem:
e = Entry(root, text = "Message").grid(row=10, column = 5)
Entry(root, text = "Message") returns a tkEntry widget.

.grid(row=10, column = 5) returns None

When you daisychain the two together, Entry(root, text = "Message").grid(row=10, column = 5 returns None. All of the variables you assigned to widgets are None. myLabel1 == None, myButton3 == None, and, of course, e == None.

You don't use any of those variables, other than e, so don't assign the widget to a variable. For e, I think you should do something different. You can directly get or set text from an Entry widget, but I would use a tkinter StringVar. Like this:
import tkinter as tk  # Do not use wildcard import

bg = "black"
fg = "white"


# It is convention to put functions near top.
def encrypt():
    """Functions should have a docstring saying what they do."""
    label.set("I clicked Encrypt!")


# Instead of creating new labels, change the text of the existing label.
def decrypt():
    """I am called by the copy button?"""
    label.set(entry.get())


root = tk.Tk()
root.configure(background=bg)

entry = tk.StringVar(root, "Message")  # Use this to get text from entry widget
tk.Entry(root, textvariable=entry).grid(row=10, column=5)

# Do not put multiple widgets in same grid location
label = tk.StringVar(root, "")  # Use this to set text of the label.
tk.Label(root, textvariable=label, bg=bg, fg=fg).grid(row=0, column=1)

# If you don't use a variable, don't create one.
tk.Label(root, text="Hello World!", bg=bg, fg=fg).grid(row=0, column=0)
tk.Label(root, text="My Name is Bob!", bg=bg, fg=fg).grid(row=1, column=1)
tk.Button(root, text="Encrypt", command=encrypt, fg=fg, bg=bg).grid(row=2, column=1)
tk.Button(root, text="Decrypt", state=tk.DISABLED, padx=50).grid(row=3, column=1)
tk.Button(root, text="Copy", command=decrypt).grid(row=4, column=1)

root.mainloop()
This is the code without using StringVar. Setting the entry text becomes difficult. I also removed the fg and bg setting stuff. If you want to change the window appearance, use ttk widets and themes. I repurposed the copy button to randomly change the theme for the window when clicked.
import tkinter as tk  # Do not use wildcard import
from tkinter import ttk
import random


def encrypt():
    label["text"] = "I clicked Encrypt!"


def decrypt():
    label["text"] = entry.get()


def change_theme():
    theme = random.choice(style.theme_names())
    label["text"] = theme
    style.theme_use(theme)


root = tk.Tk()
style = ttk.Style(root)

entry = ttk.Entry(root)
entry.insert(0, "Message")  # This is why you use a StringVar.
entry.grid(row=10, column=5)

label = ttk.Label(root, text="")
label.grid(row=0, column=1)

# If you don't use a variable, don't create one.
ttk.Label(root, text="Hello World!").grid(row=0, column=0)
ttk.Label(root, text="My Name is Bob!").grid(row=1, column=1)
ttk.Button(root, text="Encrypt", command=encrypt).grid(row=2, column=1)
ttk.Button(root, text="Decrypt", command=decrypt).grid(row=3, column=1)
ttk.Button(root, text="Copy", command=change_theme).grid(row=4, column=1)

root.mainloop()
Reply


Messages In This Thread
RE: 'NoneType' object has no attribute 'get' - by deanhystad - Oct-13-2023, 05:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter AttributeError: 'GUI' object has no attribute pfdjhfuys 3 1,720 May-18-2023, 03:30 PM
Last Post: pfdjhfuys
  [Kivy] Windows 10: AttributeError: 'WM_PenProvider' object has no attribute 'hwnd' mikepy 1 2,449 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,695 Aug-30-2022, 08:44 AM
Last Post: tompranks
  AttributeError: 'NoneType' object has no attribute 'get' George87 5 15,806 Dec-23-2021, 04:47 AM
Last Post: George87
  [PyQt] AttributeError: 'NoneType' object has no attribute 'text' speedev 9 11,665 Sep-25-2021, 06:14 PM
Last Post: Axel_Erfurt
  [Tkinter] AttributeError: '' object has no attribute 'tk' Maryan 2 14,922 Oct-29-2020, 11:57 PM
Last Post: Maryan
  [Tkinter] AttributeError: 'tuple' object has no attribute 'replace' linuxhacker 7 7,013 Aug-08-2020, 12:47 AM
Last Post: linuxhacker
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 6,376 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' edphilpot 5 9,322 Dec-20-2019, 09:52 PM
Last Post: joe_momma
  [Tkinter] AttributeError: 'App' object has no attribute 'set_text' Sahil1313 6 12,217 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