import tkinter
from tkinter import *
from tkinter import messagebox
val = ""
A = 0
operator = ""
def btn_7_isclicked():
global val
val = val + "7"
data.set(val)
def btn_8_isclicked():
global val
val = val + "8"
data.set(val)
def btn_9_isclicked():
global val
val = val + "9"
data.set(val)
def btn_4_isclicked():
global val
val = val + "4"
data.set(val)
def btn_5_isclicked():
global val
val = val + "5"
data.set(val)
def btn_6_isclicked():
global val
val = val + "6"
data.set(val)
def btn_1_isclicked():
global val
val = val + "1"
data.set(val)
def btn_2_isclicked():
global val
val = val + "2"
data.set(val)
def btn_3_isclicked():
global val
val = val + "3"
data.set(val)
def btn_0_isclicked():
global val
val = val + "0"
data.set(val)
def add_btn_isclicked():
global A
global operator
global val
A = int(val)
operator = "+"
val = val + "+"
data.set(val)
def sub_btn_isclicked():
global A
global operator
global val
A = int(val)
operator = "-"
val = val + "-"
data.set(val)
def mul_btn_isclicked():
global A
global operator
global val
A = int(val)
operator = "*"
val = val + "*"
data.set(val)
def div_btn_isclicked():
global A
global operator
global val
A = int(val)
operator = "/"
val = val + "/"
data.set(val)
val = str(val)
def clear_btn_isclicked():
global val
global A
global operator
val = ""
A = 0
operator = ""
data.set(val)
def result():
global val
global A
global operator
val2 = val
if operator == "+":
x = int((val2.split("+")[1]))
c = A + x
data.set(c)
val = str(c)
elif operator == "-":
x = int((val2.split("-")[1]))
c = A - x
data.set(c)
val = str(c)
elif operator == "*":
x = int((val2.split("*")[1]))
c = A * x
data.set(c)
val = str(c)
elif operator == "/":
x = int((val2.split("/")[1]))
if x == 0:
messagebox.showerror("Error","Division by 0 not supported")
A = 0
val = ""
data.set(val)
else:
c = int(A / x)
data.set(c)
val = str(c)
root = tkinter.Tk()
root.geometry("300x400+580+180")
root.resizable(0,0)
root.title("Python - Calculator")
data = StringVar()
lbl = Label(
root,
text = "Level",
anchor = SE,
font = ("Verdana",20),
textvariable = data,
background = "#ffffff",
fg = "#000000",
)
lbl.pack(expand = True, fill = "both")
btnrow1 = Frame(root,bg = "#000000")
btnrow1.pack(expand = True, fill = "both",)
btnrow2 = Frame(root)
btnrow2.pack(expand = True, fill = "both",)
btnrow3 = Frame(root)
btnrow3.pack(expand = True, fill = "both",)
btnrow4 = Frame(root)
btnrow4.pack(expand = True, fill = "both",)
btn7 = Button(
btnrow1,
text = "7",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_7_isclicked,
)
btn7.pack(side = LEFT, expand = True, fill = "both")
btn8 = Button(
btnrow1,
text = "8",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_8_isclicked,
)
btn8.pack(side = LEFT, expand = True, fill = "both")
btn9 = Button(
btnrow1,
text = "9",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_9_isclicked,
)
btn9.pack(side = LEFT, expand = True, fill = "both")
addbtn = Button(
btnrow1,
text = "+",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = add_btn_isclicked,
)
addbtn.pack(side = LEFT, expand = True, fill = "both")
btn4 = Button(
btnrow2,
text = "4",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_4_isclicked,
)
btn4.pack(side = LEFT, expand = True, fill = "both")
btn5 = Button(
btnrow2,
text = "5",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_5_isclicked,
)
btn5.pack(side = LEFT, expand = True, fill = "both")
btn6 = Button(
btnrow2,
text = "6",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_6_isclicked,
)
btn6.pack(side = LEFT, expand = True, fill = "both")
subbtn = Button(
btnrow2,
text = "-",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = sub_btn_isclicked,
)
subbtn.pack(side = LEFT, expand = True, fill = "both")
btn1 = Button(
btnrow3,
text = "1",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_1_isclicked,
)
btn1.pack(side = LEFT, expand = True, fill = "both")
btn2 = Button(
btnrow3,
text = "2",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_2_isclicked,
)
btn2.pack(side = LEFT, expand = True, fill = "both")
btn3 = Button(
btnrow3,
text = "3",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_3_isclicked,
)
btn3.pack(side = LEFT, expand = True, fill = "both")
mulbtn = Button(
btnrow3,
text = "x",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = mul_btn_isclicked,
)
mulbtn.pack(side = LEFT, expand = True, fill = "both")
clearbtn = Button(
btnrow4,
text = "C",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = clear_btn_isclicked,
)
clearbtn.pack(side = LEFT, expand = True, fill = "both")
btn0 = Button(
btnrow4,
text = "0",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_0_isclicked,
)
btn0.pack(side = LEFT, expand = True, fill = "both")
equalbtn = Button(
btnrow4,
text = "=",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = result,
)
equalbtn.pack(side = LEFT, expand = True, fill = "both")
divbtn = Button(
btnrow4,
text = "/",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = div_btn_isclicked,
)
divbtn.pack(side = LEFT, expand = True, fill = "both")
root.mainloop()
Output : -
from tkinter import *
from tkinter import messagebox
val = ""
A = 0
operator = ""
def btn_7_isclicked():
global val
val = val + "7"
data.set(val)
def btn_8_isclicked():
global val
val = val + "8"
data.set(val)
def btn_9_isclicked():
global val
val = val + "9"
data.set(val)
def btn_4_isclicked():
global val
val = val + "4"
data.set(val)
def btn_5_isclicked():
global val
val = val + "5"
data.set(val)
def btn_6_isclicked():
global val
val = val + "6"
data.set(val)
def btn_1_isclicked():
global val
val = val + "1"
data.set(val)
def btn_2_isclicked():
global val
val = val + "2"
data.set(val)
def btn_3_isclicked():
global val
val = val + "3"
data.set(val)
def btn_0_isclicked():
global val
val = val + "0"
data.set(val)
def add_btn_isclicked():
global A
global operator
global val
A = int(val)
operator = "+"
val = val + "+"
data.set(val)
def sub_btn_isclicked():
global A
global operator
global val
A = int(val)
operator = "-"
val = val + "-"
data.set(val)
def mul_btn_isclicked():
global A
global operator
global val
A = int(val)
operator = "*"
val = val + "*"
data.set(val)
def div_btn_isclicked():
global A
global operator
global val
A = int(val)
operator = "/"
val = val + "/"
data.set(val)
val = str(val)
def clear_btn_isclicked():
global val
global A
global operator
val = ""
A = 0
operator = ""
data.set(val)
def result():
global val
global A
global operator
val2 = val
if operator == "+":
x = int((val2.split("+")[1]))
c = A + x
data.set(c)
val = str(c)
elif operator == "-":
x = int((val2.split("-")[1]))
c = A - x
data.set(c)
val = str(c)
elif operator == "*":
x = int((val2.split("*")[1]))
c = A * x
data.set(c)
val = str(c)
elif operator == "/":
x = int((val2.split("/")[1]))
if x == 0:
messagebox.showerror("Error","Division by 0 not supported")
A = 0
val = ""
data.set(val)
else:
c = int(A / x)
data.set(c)
val = str(c)
root = tkinter.Tk()
root.geometry("300x400+580+180")
root.resizable(0,0)
root.title("Python - Calculator")
data = StringVar()
lbl = Label(
root,
text = "Level",
anchor = SE,
font = ("Verdana",20),
textvariable = data,
background = "#ffffff",
fg = "#000000",
)
lbl.pack(expand = True, fill = "both")
btnrow1 = Frame(root,bg = "#000000")
btnrow1.pack(expand = True, fill = "both",)
btnrow2 = Frame(root)
btnrow2.pack(expand = True, fill = "both",)
btnrow3 = Frame(root)
btnrow3.pack(expand = True, fill = "both",)
btnrow4 = Frame(root)
btnrow4.pack(expand = True, fill = "both",)
btn7 = Button(
btnrow1,
text = "7",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_7_isclicked,
)
btn7.pack(side = LEFT, expand = True, fill = "both")
btn8 = Button(
btnrow1,
text = "8",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_8_isclicked,
)
btn8.pack(side = LEFT, expand = True, fill = "both")
btn9 = Button(
btnrow1,
text = "9",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_9_isclicked,
)
btn9.pack(side = LEFT, expand = True, fill = "both")
addbtn = Button(
btnrow1,
text = "+",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = add_btn_isclicked,
)
addbtn.pack(side = LEFT, expand = True, fill = "both")
btn4 = Button(
btnrow2,
text = "4",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_4_isclicked,
)
btn4.pack(side = LEFT, expand = True, fill = "both")
btn5 = Button(
btnrow2,
text = "5",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_5_isclicked,
)
btn5.pack(side = LEFT, expand = True, fill = "both")
btn6 = Button(
btnrow2,
text = "6",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_6_isclicked,
)
btn6.pack(side = LEFT, expand = True, fill = "both")
subbtn = Button(
btnrow2,
text = "-",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = sub_btn_isclicked,
)
subbtn.pack(side = LEFT, expand = True, fill = "both")
btn1 = Button(
btnrow3,
text = "1",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_1_isclicked,
)
btn1.pack(side = LEFT, expand = True, fill = "both")
btn2 = Button(
btnrow3,
text = "2",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_2_isclicked,
)
btn2.pack(side = LEFT, expand = True, fill = "both")
btn3 = Button(
btnrow3,
text = "3",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_3_isclicked,
)
btn3.pack(side = LEFT, expand = True, fill = "both")
mulbtn = Button(
btnrow3,
text = "x",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = mul_btn_isclicked,
)
mulbtn.pack(side = LEFT, expand = True, fill = "both")
clearbtn = Button(
btnrow4,
text = "C",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = clear_btn_isclicked,
)
clearbtn.pack(side = LEFT, expand = True, fill = "both")
btn0 = Button(
btnrow4,
text = "0",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = btn_0_isclicked,
)
btn0.pack(side = LEFT, expand = True, fill = "both")
equalbtn = Button(
btnrow4,
text = "=",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = result,
)
equalbtn.pack(side = LEFT, expand = True, fill = "both")
divbtn = Button(
btnrow4,
text = "/",
font = ("Verdana",22),
relief = GROOVE,
border = 0,
command = div_btn_isclicked,
)
divbtn.pack(side = LEFT, expand = True, fill = "both")
root.mainloop()
Output : -
0 Comments
Post a Comment