forked from MoKhajavi75/Basic_Calculator_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.py
126 lines (90 loc) · 3.17 KB
/
GUI.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
# Basic Calculator GUI by MohamadKh75
# 2017-06-02
# ********************
from tkinter import *
from tkinter import messagebox
from pathlib import Path
# Defining Functions
def sum_fields():
if e1.get() == "" or e2.get() == "":
messagebox.showerror("Error", "Nothing To Do!")
else:
result = int((e1.get())) + int((e2.get()))
result = str(result)
res.configure(text=result)
def sub_fields():
if e1.get() == "" or e2.get() == "":
messagebox.showerror("Error", "Nothing To Do!")
else:
result = int((e1.get())) - int((e2.get()))
result = str(result)
res.configure(text=result)
def multi_fields():
if e1.get() == "" or e2.get() == "":
messagebox.showerror("Error", "Nothing To Do!")
else:
result = int((e1.get())) * int((e2.get()))
result = str(result)
res.configure(text=result)
def dev_fields():
if e1.get() == "" or e2.get() == "":
messagebox.showerror("Error", "Nothing To Do!")
else:
result = int((e1.get())) / int((e2.get()))
result = str(result)
res.configure(text=result)
# Defining Paths
img_path = Path("Others\calculator.png").resolve()
icon_path = Path("Others\calculator.ico").resolve()
# Create a window
root = Tk()
root.title("Basic Calculator")
root.iconbitmap(icon_path)
# Set the right side image (Calculator)
logo = PhotoImage(file=img_path)
w1 = Label(root, image=logo).pack(side=RIGHT)
# Set the title bar
w2 = Label(root, padx=75, pady=5, text="Basic Calculator",
fg="Green", bg="Black", font="TimesNewRoman 15 bold").pack(side=TOP)
# Create the first row
row = Frame(root)
lab = Label(row, width=15, text="First Number:")
e1 = Entry(row)
e1.insert(0, "")
row.pack(side=TOP, fill=X, padx=5, pady=5)
lab.pack(side=LEFT)
e1.pack(side=RIGHT, expand=YES, fill=X)
# Create the second row
row = Frame(root)
lab = Label(row, width=15, text="Second Number:")
e2 = Entry(row)
e2.insert(1, "")
row.pack(side=TOP, fill=X, padx=5, pady=5)
lab.pack(side=LEFT)
e2.pack(side=RIGHT, expand=YES, fill=X)
# Just a thick horizontal line!
v1 = Label(root, text="", fg="Green", bg="Black")
v1.pack(fill=X, pady=10)
# Summation Button
b1 = Button(root, text='+', font="TimesNewRoman 17 bold", command=sum_fields, padx=20, pady=20)
b1.pack(side=TOP, padx=20, pady=5, fill=X)
# Subtraction Button
b2 = Button(root, text='-', font="TimesNewRoman 17 bold", command=sub_fields, padx=20, pady=20)
b2.pack(side=TOP, padx=20, pady=5, fill=X)
# Multiple Button
b3 = Button(root, text='x', font="TimesNewRoman 17 bold", command=multi_fields, padx=20, pady=20)
b3.pack(side=TOP, padx=20, pady=5, fill=X)
# Divide Button
b4 = Button(root, text='/', font="TimesNewRoman 17 bold", command=dev_fields, padx=20, pady=20)
b4.pack(side=TOP, padx=20, pady=5, fill=X)
# Just a thick horizontal line!
w = Label(root, text="", fg="Green", bg="Black")
w.pack(fill=X, pady=10)
# The BIG result label
v2 = Label(root, text="Result:", fg="Black", font="TimesNewRoman 15 bold")
v2.pack(side=LEFT, padx=20, pady=10)
# And the calculated result
res = Label(root, fg="Black", font="TimesNewRoman 15 bold")
res.pack(side=LEFT, padx=40, pady=10)
# Mainloop
root.mainloop()