-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator2.py
114 lines (82 loc) · 3.13 KB
/
Calculator2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from tkinter import *
import tkinter.messagebox
# ========================== settings ========================
root = Tk()
root.geometry('400x200')
root.title('calculator')
root.resizable(width=False, height=False)
color = 'gray'
root.configure(bg=color)
# ========================== variables ==========================
num1 = StringVar()
num2 = StringVar()
res_value = StringVar()
# ========================== frames ==========================
top_first = Frame(root, width=400, height=50, bg=color)
top_first.pack(side=TOP)
top_second = Frame(root, width=400, height=50, bg=color)
top_second.pack(side=TOP)
top_third = Frame(root, width=400, height=50, bg=color)
top_third.pack(side=TOP)
top_forth = Frame(root, width=400, height=50, bg=color)
top_forth.pack(side=TOP)
# ========================== Functions ==========================
def errorMsg(ms):
if ms == 'error':
tkinter.messagebox.showerror('Error', 'something went wrong')
elif ms == 'division zero error':
tkinter.messagebox.showerror('Division Error', 'Can Not Divide By 0')
def plus():
try:
value = float(num1.get()) + float(num2.get())
res_value.set(value)
except:
errorMsg('error')
def minus():
try:
value = float(num1.get()) - float(num2.get())
res_value.set(value)
except:
errorMsg('error')
def mul():
try:
value = float(num1.get()) * float(num2.get())
res_value.set(value)
except:
errorMsg('error')
def div():
if num2.get() == '0':
errorMsg('division zero error')
elif num2.get() != '0':
try:
value = float(num1.get()) / float(num2.get())
res_value.set(value)
except:
errorMsg('error')
# ========================== Buttons ==========================
btn_plus = Button(top_third, text="+", width=10,
highlightbackground=color, command=lambda: plus())
btn_plus.pack(side=LEFT, padx=10, pady=10)
btn_minus = Button(top_third, text="-", width=10,
highlightbackground=color, command=lambda: minus())
btn_minus.pack(side=LEFT, padx=10, pady=10)
btn_mul = Button(top_third, text="*", width=10,
highlightbackground=color, command=lambda: mul())
btn_mul.pack(side=LEFT, padx=10, pady=10)
btn_div = Button(top_third, text="/", width=10,
highlightbackground=color, command=lambda: div())
btn_div.pack(side=LEFT, padx=10, pady=10)
# ========================== Entries and Labels ==========================
label_first_num = Label(top_first, text='Input Number 1: ', bg=color)
label_first_num.pack(side=LEFT, pady=10, padx=10)
first_num = Entry(top_first, highlightbackground=color, textvariable=num1)
first_num.pack(side=LEFT)
label_second_num = Label(top_second, text='Input Number 2: ', bg=color)
label_second_num.pack(side=LEFT, pady=10, padx=10)
second_num = Entry(top_second, highlightbackground=color, textvariable=num2)
second_num.pack(side=LEFT)
res = Label(top_forth, text='Result: ', bg=color)
res.pack(side=LEFT, pady=10, padx=10)
res_num = Entry(top_forth, highlightbackground=color, textvariable=res_value)
res_num.pack(side=LEFT)
root.mainloop()