Error: AttributeError: ‘_tkinter.tkapp’ object has no attribute ‘insert’

Total
1
Shares

guys. Help me, please. I’ve got a problem with calculator in tkinter.

Error: AttributeError: ‘_tkinter.tkapp’ object has no attribute ‘insert’.

from tkinter import *



root = Tk()
root.title("Calculator")
root.geometry("400x480")
root.configure(bg = 'gray')


#commands for buttons

def add_digit(digit):
    root.insert(0, digit)

#buttons
b7 = Button(root, text = '7', bd = 5, command = lambda : add_digit(7)).grid(row = 1, column = 0, stick = 'wens', padx = 5, pady = 5)
b8 = Button(root, text = '8', bd = 5, command = lambda : add_digit(8)).grid(row = 1, column = 1, stick = 'wens', padx = 5, pady = 5)
b9 = Button(root, text = '9', bd = 5, command = lambda : add_digit(9)).grid(row = 1, column = 2, stick = 'wens', padx = 5, pady = 5)
b4 = Button(root, text = '4', bd = 5, command = lambda : add_digit(4)).grid(row = 2, column = 0, stick = 'wens', padx = 5, pady = 5)
b5 = Button(root, text = '5', bd = 5, command = lambda : add_digit(5)).grid(row = 2, column = 1, stick = 'wens', padx = 5, pady = 5)
b6 = Button(root, text = '6', bd = 5, command = lambda : add_digit(6)).grid(row = 2, column = 2, stick = 'wens', padx = 5, pady = 5)
b1 = Button(root, text = '1', bd = 5, command = lambda : add_digit(1)).grid(row = 3, column = 0, stick = 'wens', padx = 5, pady = 5)
b2 = Button(root, text = '2', bd = 5, command = lambda : add_digit(2)).grid(row = 3, column = 1, stick = 'wens', padx = 5, pady = 5)
b3 = Button(root, text = '3', bd = 5, command = lambda : add_digit(3)).grid(row = 3, column = 2, stick = 'wens', padx = 5, pady = 5)
b0 = Button(root, text = '0', bd = 5, command = lambda : add_digit(0)).grid(row = 4, column = 1, stick = 'wens', padx = 5, pady = 5)

#entry

entr = Entry(root, bd = 4).grid(row = 0, column = 0, columnspan = 3)


root.mainloop()


Solution

You cannot ‘insert’ text into a Window. A Label is used for text on a window. Why do you want to add numbers to the root window when you have a text widget? Also, update your function:

def add_digit(digit):
    entr.insert(END, digit) #=== Insert into text widget

Also, the code which has your entry should be:

entr = Entry(root, bd = 4)
entr.grid(row = 0, column = 0, columnspan = 3)
Leave a Reply

Your email address will not be published. Required fields are marked *