0% found this document useful (0 votes)
15 views

Ma Code

This document defines the code for a basic calculator application created with Tkinter. It defines functions for number buttons, arithmetic operators, and clear/equal buttons. Global variables are used to track the current operation and first/second numbers. The buttons are created and placed in a grid layout. When clicked, the buttons insert numbers or call functions for operations. The equal button calls a calculation function to evaluate the operation on the stored numbers.

Uploaded by

Nhật Minh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Ma Code

This document defines the code for a basic calculator application created with Tkinter. It defines functions for number buttons, arithmetic operators, and clear/equal buttons. Global variables are used to track the current operation and first/second numbers. The buttons are created and placed in a grid layout. When clicked, the buttons insert numbers or call functions for operations. The equal button calls a calculation function to evaluate the operation on the stored numbers.

Uploaded by

Nhật Minh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

from tkinter import *

import math
a = Tk()
a.title('Casio')
operator = 0
e = Entry(width=20,bd = 8, font='Arial 17',fg =
'red',justify='center',foreground='sky blue',background='light yellow')
e.grid(row=0,column=0,columnspan=4,pady=(10,25))
def click(number):
e.insert(END,number)
def dot():
e.insert(END,'.')
def clear():
e.delete(0,END)
def xoa():
e.delete(len(e.get())-1)
def addition():
global num1
if e.get() == '':
num1 = 0
else:
num1 = float(e.get())
e.delete(0,END)
global operator
operator = 1
def sub():
global num1
if e.get() == '':
num1 = 0
else:
num1 = float(e.get())
e.delete(0,END)
global operator
operator = 2
def mul():
global num1
if e.get() == '':
num1 = 0
else:
num1 = float(e.get())
e.delete(0,END)
global operator
operator = 3
def div():
global num1
if e.get() == '':
num1 = 0
else:
num1 = float(e.get())
e.delete(0,END)
global operator
operator = 4
def percent():
global num1
if e.get() == '':
num1 = 0
else:
num1 = float(e.get())
e.delete(0, END)
global operator
operator = 5
def fraction():
global num1
if e.get() == '':
num1 = 0
else:
num1 = float(e.get())
e.delete(0, END)
global operator
operator = 6
def caculation():
global num1
if e.get() == '':
num2 = 0
else:
num2 = float(e.get())
e.delete(0, END)
if operator == 1:
e.insert(0, round(num1 + num2))
if operator == 2:
e.insert(0, round(num1 - num2))
if operator == 3:
e.insert(0, round(num1 * num2))
if operator == 4:
if num2 == 0:
e.insert(0, 'ERROR:Cant divide by 0.')
else:
e.insert(0, round(num1 / num2))
if operator == 5:
e.insert(0, str(round((num1 / num2)*100))+'%')
if operator == 6:
e.insert(0, (1/num1))
bt_7 = Button(text='7',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: click(7))
bt_8 = Button(text='8',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: click(8))
bt_9 = Button(text='9',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: click(9))
bt_div = Button(text='/',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: div())
bt_4 = Button(text='4',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: click(4))
bt_5 = Button(text='5',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: click(5))
bt_6 = Button(text='6',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: click(6))
bt_mul = Button(text='*',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: mul())
bt_1 = Button(text='1',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: click(1))
bt_2 = Button(text='2',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: click(2))
bt_3 = Button(text='3',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: click(3))
bt_sub = Button(text='-',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: sub())
bt_clear = Button(text='C',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: clear())
bt_0 = Button(text='0',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda: click(0))
bt_eq = Button(text='=',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda :caculation())
bt_add = Button(text='+',width=5,height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda :addition())
bt_clear2 = Button(text='CE',width=5, height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda :xoa())
bt_pc = Button(text='%',width=5, height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda :percent())
bt_fract = Button(text='1/x',width=5, height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda :fraction())
bt_dot = Button(text='.',width=5, height=2,font='Arial
13',foreground='forestgreen',background='beige',command=lambda :dot())
bt_clear2.grid(row=5,column=0,padx=5,pady=5)
bt_7.grid(row=1,column=0,padx=5,pady=5)
bt_8.grid(row=1,column=1,padx=5,pady=5)
bt_9.grid(row=1,column=2,padx=5,pady=5)
bt_div.grid(row=1,column=3,padx=5,pady=5)
bt_4.grid(row=2,column=0,padx=5,pady=5)
bt_5.grid(row=2,column=1,padx=5,pady=5)
bt_6.grid(row=2,column=2,padx=5,pady=5)
bt_mul.grid(row=2,column=3,padx=5,pady=5)
bt_1.grid(row=3,column=0,padx=5,pady=5)
bt_2.grid(row=3,column=1,padx=5,pady=5)
bt_3.grid(row=3,column=2,padx=5,pady=5)
bt_sub.grid(row=3,column=3,padx=5,pady=5)
bt_clear.grid(row=4,column=0,padx=5,pady=5)
bt_0.grid(row=4,column=1,padx=5,pady=5)
bt_eq.grid(row=4,column=2,padx=5,pady=5)
bt_add.grid(row=4,column=3,padx=5,pady=5)
bt_pc.grid(row=5,column=3,padx=5,pady=5)
bt_fract.grid(row=5,column=2,padx=5,pady=5)
bt_dot.grid(row=5,column=1,padx=5,pady=5)
a.mainloop()

You might also like