Search
 
SCRIPT & CODE EXAMPLE
 

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()
Comment

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()
Comment

PREVIOUS NEXT
Code Example
Python :: is plaindrome python 
Python :: qdate to date 
Python :: how to round a number up in python 
Python :: pandas convert column to title case 
Python :: pyfiglet not coming up 
Python :: all combinations 
Python :: df to dict 
Python :: function annotation 
Python :: how to play audio in python using pygame 
Python :: how to check if text is lower in python 
Python :: hwo to except every error in python try statemen 
Python :: support vector machine example 
Python :: Django - Knox auth setup 
Python :: python source code 
Python :: how to mention a role discord.py 
Python :: lru_cache 
Python :: Total processing python 
Python :: django upload multiple files 
Python :: python iteration 
Python :: using hashlib module in python 
Python :: create 20 char with python 
Python :: Convert Int to String Using format() method 
Python :: how to find highest number in list python 
Python :: pandas multiply all dataframe 
Python :: assert with message python 
Python :: semicolon python 
Python :: python last index of item in list 
Python :: boto3 rename file s3 
Python :: python typing module list 
Python :: extracting values in pandas 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =