-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.py
129 lines (100 loc) · 3.57 KB
/
App.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'''
This script was written in python 3.x.
In order to run this script, please make sure your python version is 3.x or above.
How to run:
python App.py
or if it doesn't work use this one:
python3 App.py
Author: Udin <[email protected]>
'''
from tkinter import *
import tkinter.messagebox
from tkinter.ttk import Frame, Label, Entry
class App(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Calculator by Udin")
self.pack(fill=BOTH, expand=True)
global value
value = 0
global num1
num1 = StringVar()
global num2
num2 = StringVar()
global res
res = StringVar()
frame1 = Frame(self)
frame1.pack(fill=X)
lbl1 = Label(frame1, text="Input Number 1 :", width=15)
lbl1.pack(side=LEFT, padx=5, pady=5)
entry1 = Entry(frame1,textvariable=num1)
entry1.pack(fill=X, padx=5, expand=True)
frame2 = Frame(self)
frame2.pack(fill=X)
lbl2 = Label(frame2, text="Input Number 2 :", width=15)
lbl2.pack(side=LEFT, padx=5, pady=5)
entry2 = Entry(frame2,textvariable=num2)
entry2.pack(fill=X, padx=5, expand=True)
frame3 = Frame(self)
frame3.pack(fill=X)
btnplus = Button(frame3, text="+", width=8, command=self.plus)
btnplus.pack(side=LEFT, anchor=N, padx=5, pady=5)
btnminus = Button(frame3, text="-", width=8, command=self.minus)
btnminus.pack(side=LEFT, anchor=N, padx=5, pady=5)
btnmul = Button(frame3, text="*", width=8, command=self.mul)
btnmul.pack(side=LEFT, anchor=N, padx=5, pady=5)
btndiv = Button(frame3, text="/", width=8, command=self.div)
btndiv.pack(side=LEFT, anchor=N, padx=5, pady=5)
frame4 = Frame(self)
frame4.pack(fill=X)
lbl3 = Label(frame4, text="Result :", width=10)
lbl3.pack(side=LEFT, padx=5, pady=5)
result = Entry(frame4,textvariable=res)
result.pack(fill=X, padx=5, expand=True)
def errorMsg(self,msg):
if msg == 'error':
tkinter.messagebox.showerror('Error!', 'Something went wrong! Maybe invalid entries')
elif msg == 'divisionerror':
tkinter.messagebox.showerror('Division Error', 'The value of input number 2 is 0. No dividing by 0')
def plus(self):
try:
value = float(num1.get()) + float(num2.get())
res.set(self.makeAsItIs(value))
except:
self.errorMsg('error')
def minus(self):
try:
value = float(num1.get()) - float(num2.get())
res.set(self.makeAsItIs(value))
except:
self.errorMsg('error')
def mul(self):
try:
value = float(num1.get()) * float(num2.get())
res.set(self.makeAsItIs(value))
except:
self.errorMsg('error')
def div(self):
# checks if user is trying to divide by zero
if num2.get() == '0':
self.errorMsg('divisionerror')
elif num2.get() != '0':
try:
value = float(num1.get()) / float(num2.get())
res.set(self.makeAsItIs(value))
except:
self.errorMsg('error')
def makeAsItIs(self, value):
if (value == int(value)):
value = int(value)
return value
def main():
root = Tk()
root.geometry("300x140")
app = App(root)
root.mainloop()
if __name__ == '__main__':
main()