Python Forum

Full Version: .delete and .insert not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
from tkinter import *


root = Tk()

# Display
root.title("Simple Calculator")
display = Entry(root, width=25).grid(row=0, column=0, columnspan=3, padx=10, pady=20)


# commands
def button_click(number):
    display.delete(0, END)
    display.insert(0, number)


# buttons numbers
button_1 = Button(root, text="1", padx=20, pady=15, command=lambda: button_click(1)).grid(row=3, column=0)
button_2 = Button(root, text="2", padx=20, pady=15, command=lambda: button_click(2)).grid(row=3, column=1)
button_3 = Button(root, text="3", padx=20, pady=15, command=lambda: button_click(3)).grid(row=3, column=2)
button_4 = Button(root, text="4", padx=20, pady=15, command=lambda: button_click(4)).grid(row=2, column=0)
button_5 = Button(root, text="5", padx=20, pady=15, command=lambda: button_click(5)).grid(row=2, column=1)
button_6 = Button(root, text="6", padx=20, pady=15, command=lambda: button_click(6)).grid(row=2, column=2)
button_7 = Button(root, text="7", padx=20, pady=15, command=lambda: button_click(7)).grid(row=1, column=0)
button_8 = Button(root, text="8", padx=20, pady=15, command=lambda: button_click(8)).grid(row=1, column=1)
button_9 = Button(root, text="9", padx=20, pady=15, command=lambda: button_click(9)).grid(row=1, column=2)
button_0 = Button(root, text="0", padx=20, pady=15, command=lambda: button_click(0)).grid(row=4, column=0)

# buttons functions
button_add = Button(root, text="+", padx=19, pady=15, command=button_click).grid(row=5, column=0)
button_equal = Button(root, text="=", padx=56, pady=15, command=button_click).grid(row=5, column=1, columnspan=2)
button_clear = Button(root, text="CLEAR", padx=42, pady=15, command=button_click).grid(row=4, column=1, columnspan=2)

root.mainloop()
Error:
Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.8/tkinter/__init__.py", line 1883, in __call__ return self.func(*args) File "/run/media/francois/Bulk Storage/PycharmProjects/test1/Tkinter_GUI.py", line 20, in <lambda> button_3 = Button(root, text="3", padx=20, pady=15, command=lambda: button_click(3)).grid(row=3, column=2) File "/run/media/francois/Bulk Storage/PycharmProjects/test1/Tkinter_GUI.py", line 13, in button_click display.delete(0, END) AttributeError: 'NoneType' object has no attribute 'delete'
I did check with this Python website's Tkinter page:https://realpython.com/python-gui-tkinter
but I couldn't find what I did wrong.
You need to get the reference to the object Entry, by calling .grid on it you lose it, call grid separately.
change
display = Entry(root, width=25).grid(row=0, column=0, columnspan=3, padx=10, pady=20)
to

display = Entry(root, width=25)
display.grid(row=0, column=0, columnspan=3, padx=10, pady=20)
I guess grid returns None so display is None? I assume that's because it actually changes the Entry so it doesn't need to return a value? Check the documentation to be sure. With that in mind, should you actually be writing

display = Entry(root, width=25)
display.grid(row=0, column=0, columnspan=3, padx=10, pady=20)

Presumably your buttons are going to suffer from the same problem.
Oh ok I see. It's now working thanks!