-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.py
More file actions
157 lines (126 loc) · 7.01 KB
/
ui.py
File metadata and controls
157 lines (126 loc) · 7.01 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import tkinter
from tkinter import *
import functions
from graph import plot_equation
FONT = ("Arial", 14, "bold")
# Color Palette
BG_COLOR = "#1e1e1e"
ENTRY_BG = "#2e2e2e"
ENTRY_FG = "#ffffff"
BTN_COLOR_NUM = "#3c3f41"
BTN_COLOR_OP = "#f39c12"
BTN_COLOR_FUNC = "#3498db"
BTN_COLOR_CTRL = "#e74c3c"
BTN_COLOR_EQ = "#2ecc71"
BTN_HOVER = "#555"
class CalculatorButton():
def __init__(self, master, text, row, col, colspan=1, width=5, height=2, command=None, bg=BTN_COLOR_NUM):
self.button = Button(master,
text=text,
width=width,
height=height,
font=FONT,
bg=bg,
fg="white",
activebackground=BTN_HOVER,
relief="flat",
command=command)
self.button.grid(row=row, column=col, columnspan=colspan, padx=2, pady=2, sticky="nsew")
class CalculatorUI():
def __init__(self):
self.window = Tk()
self.window.title("Scientific Calculator")
self.window.config(padx=20, pady=20, bg=BG_COLOR)
self.expression = ""
self.input_text = tkinter.StringVar()
self.create_ui()
def create_ui(self):
title = Label(text="Scientific Calculator", font=("Arial", 18, "bold"), bg=BG_COLOR, fg="white")
title.grid(row=0, column=0, columnspan=4, pady=(0, 10))
display_eq = Entry(font=("Arial", 18), width=25, bg=ENTRY_BG, fg=ENTRY_FG,
insertbackground="white", borderwidth=3, relief="sunken",
justify="right", textvariable=self.input_text)
display_eq.grid(row=1, column=0, columnspan=4, padx=10, pady=10, sticky="ew")
# Row 1
CalculatorButton(self.window, "AC", 2, 0, bg=BTN_COLOR_CTRL, command=self.clear)
CalculatorButton(self.window, "DEL", 2, 1, bg=BTN_COLOR_CTRL, command=self.delete)
CalculatorButton(self.window, "(", 2, 2, bg=BTN_COLOR_OP, command=lambda: self.append_number("("))
CalculatorButton(self.window, ")", 2, 3, bg=BTN_COLOR_OP, command=lambda: self.append_number(")"))
# Row 2
CalculatorButton(self.window, "sin", 3, 0, bg=BTN_COLOR_FUNC, command=lambda: self.append_function("sin"))
CalculatorButton(self.window, "cos", 3, 1, bg=BTN_COLOR_FUNC, command=lambda: self.append_function("cos"))
CalculatorButton(self.window, "tan", 3, 2, bg=BTN_COLOR_FUNC, command=lambda: self.append_function("tan"))
CalculatorButton(self.window, "^", 3, 3, bg=BTN_COLOR_OP, command=lambda: self.append_operator("^"))
# Row 3
CalculatorButton(self.window, "log", 4, 0, bg=BTN_COLOR_FUNC, command=lambda: self.append_function("log"))
CalculatorButton(self.window, "ln", 4, 1, bg=BTN_COLOR_FUNC, command=lambda: self.append_function("ln"))
CalculatorButton(self.window, "x²", 4, 2, bg=BTN_COLOR_FUNC, command=lambda: self.append_function("x²"))
CalculatorButton(self.window, "√", 4, 3, bg=BTN_COLOR_FUNC, command=lambda: self.append_function("sqrt"))
# Row 4
CalculatorButton(self.window, "π", 5, 0, bg=BTN_COLOR_FUNC, command=lambda: self.append_number("π"))
CalculatorButton(self.window, "e", 5, 1, bg=BTN_COLOR_FUNC, command=lambda: self.append_number("e"))
CalculatorButton(self.window, "1/x", 5, 2, bg=BTN_COLOR_FUNC, command=lambda: self.append_function("1/"))
CalculatorButton(self.window, "x!", 5, 3, bg=BTN_COLOR_FUNC, command=lambda: self.append_function("!"))
# Row 5
CalculatorButton(self.window, "x", 6, 0, command=lambda: self.append_number("x"))
CalculatorButton(self.window, "y", 6, 1, command=lambda: self.append_number("y"))
CalculatorButton(self.window, "%", 6, 2, bg=BTN_COLOR_OP, command=lambda: self.append_operator("%"))
CalculatorButton(self.window, "÷", 6, 3, bg=BTN_COLOR_OP, command=lambda: self.append_operator("÷"))
# Row 6
CalculatorButton(self.window, "7", 7, 0, command=lambda: self.append_number("7"))
CalculatorButton(self.window, "8", 7, 1, command=lambda: self.append_number("8"))
CalculatorButton(self.window, "9", 7, 2, command=lambda: self.append_number("9"))
CalculatorButton(self.window, "×", 7, 3, bg=BTN_COLOR_OP, command=lambda: self.append_operator("×"))
# Row 7
CalculatorButton(self.window, "4", 8, 0, command=lambda: self.append_number("4"))
CalculatorButton(self.window, "5", 8, 1, command=lambda: self.append_number("5"))
CalculatorButton(self.window, "6", 8, 2, command=lambda: self.append_number("6"))
CalculatorButton(self.window, "-", 8, 3, bg=BTN_COLOR_OP, command=lambda: self.append_operator("-"))
# Row 8
CalculatorButton(self.window, "1", 9, 0, command=lambda: self.append_number("1"))
CalculatorButton(self.window, "2", 9, 1, command=lambda: self.append_number("2"))
CalculatorButton(self.window, "3", 9, 2, command=lambda: self.append_number("3"))
CalculatorButton(self.window, "+", 9, 3, bg=BTN_COLOR_OP, command=lambda: self.append_operator("+"))
# Row 9
CalculatorButton(self.window, "0", 10, 0, command=lambda: self.append_number("0"))
CalculatorButton(self.window, ".", 10, 1, command=lambda: self.append_number("."))
CalculatorButton(self.window, "=", 10, 2, 2, width=14, bg=BTN_COLOR_EQ, command=self.calculate)
# Row 10
CalculatorButton(self.window, "Graph", 11, 0, 4, width=32, bg="#8e44ad", command=self.graph_equation)
self.window.mainloop()
def append_number(self, num):
self.expression += str(num)
self.input_text.set(self.expression)
def append_operator(self, operator):
if self.expression and self.expression[-1] not in '+-×÷^':
self.expression += operator
self.input_text.set(self.expression)
def append_function(self, func):
if func == "sqrt":
self.expression += "sqrt("
elif func == '!':
self.expression += "!"
else:
self.expression += ( func + "(" )
self.input_text.set(self.expression)
def clear(self):
self.expression = ""
self.input_text.set(self.expression)
def delete(self):
self.expression = self.expression[:-1]
self.input_text.set(self.expression)
def calculate(self):
try:
result = functions.evaluate_expression(self.expression)
self.expression = str(result)
self.input_text.set(self.expression)
except Exception:
self.expression = ""
self.input_text.set("Error")
def get_equation(self):
equation = self.input_text.get()
return equation
def graph_equation(self):
equation = self.get_equation()
if equation:
plot_equation(equation)