Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python calculator source code

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()
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #python #calculator #source #code
ADD COMMENT
Topic
Name
2+1 =