My calculator program in Python allows character inputs
Question: I have created a Calculator program in Python using Tkinter and it is working
fully; but when I run it you can click on the Entry box at the top of my
Calculator and enter characters. I have written code so that when this happens
an Error occurs but these characters still appear. I would appreciate any help
on whether I can disallow this from happening.

    
    
    import tkinter as tk
    
    root=tk.Tk()
    
    root.geometry("198x125")
    root.title("Calculator")
    display= tk.Entry(root, width=33, bg="pink")
    display.grid(row=1, column=0, columnspan=5)
    
    def click(key):
        if key =='=':
            str1="123456789"
            if display.get() [0] not in str1:
                display.insert(tk.END, "First character is not valid")
            try:
                result=eval(display.get())
                display.insert(tk.END, "="+str(result))
            except:
                display.insert(tk.END, "--> ERROR")
        elif key== 'C':
            display.delete(0, tk.END)
            if '=' in display.get():
                entry.display(0, tk.END)
        else:
            if '=' in display.get():
                display.get(0, tk.END)
            display.insert(tk.END, key)
    
    button_list = [
    '7','8','9','/',
    '4','5','6','*',
    '1','2','3','-',
    '0','+','C','=',]
    r = 2
    c = 0
    for t in button_list:
        cmd=lambda x=t: click(x)
        tk.Button(root, text=t, width=5, command=cmd).grid(row=r, column=c)
        c +=1
        if c > 3:
            c = 0
            r +=1
    
    root.mainloop()
    


Answer: Use the `state=DISABLED` option to disable the Entry while it's idle, and if
you want to retain the color, just use the `disabledbackground` option:

    
    
    display= tk.Entry(root, width=33, bg="pink", disabledbackground="pink", state=tk.DISABLED)
    

Then access its state and change it to normal at the start of the click
function, and back to disabled at the end.

Refer to: <http://effbot.org/tkinterbook/entry.htm#Tkinter.Entry.config-
method>

