This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
261 lines (188 loc) · 7.64 KB
/
main.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import tkinter as tk
from tkinter import font as tkfont
import matplotlib
# import matplotlib.pyplot as plt
import sympy as sp
# from sympy import Pow, sin, diff
# from IPython.display import display, Math
import sympy.plotting.plot as symplot
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
matplotlib.use('TkAgg')
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Arial', size=14, weight="bold")
# the container is for frame stacking, tkraise to show frame.
container = tk.Frame(self)
container.grid(row=1, column=1)
self.frames = {}
for F in (Menu, Indefinite, Definite, Differential):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# all frames in the exact same spot
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("Menu")
def show_frame(self, page_name):
# show frame for the given page name
frame = self.frames[page_name]
frame.tkraise()
class Menu(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Menu", font=controller.title_font).pack()
button1 = tk.Button(self, text="Indefinite Integrals",
command=lambda: controller.show_frame("Indefinite"))
button2 = tk.Button(self, text="Definite Integrals",
command=lambda: controller.show_frame("Definite"))
button3 = tk.Button(self, text="Differentials",
command=lambda: controller.show_frame("Differential"))
button1.pack()
button2.pack()
button3.pack()
class Differential(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Derivatives", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
back_button = tk.Button(self, text="Back",
command=lambda: controller.show_frame("Menu"))
def derive(expr):
return sp.latex(sp.diff(expr))
def graph(event=None):
i = entry.get()
tmptext = derive(i)
tmptext = "$ \\frac{dy}{dx}" + sp.latex(sp.sympify(i)) + "=" + tmptext + "$"
ax.clear()
if 120 <= len(tmptext) <= 122:
fs = 12
elif 122 < len(tmptext) <= 140:
fs = 11
print(len(tmptext))
else:
fs = 16
ax.text(0.05, .4, tmptext, fontsize=fs)
canvas.draw()
symplot(sp.diff(i))
# symplot(i)
entry = tk.Entry(self, width=70)
entry.pack()
tk.Button(self, text="Differentiate", command=graph).pack()
label = tk.Label(self)
label.pack()
fig = matplotlib.figure.Figure(figsize=(10, 1), dpi=80)
ax = fig.add_subplot(111)
canvas = FigureCanvasTkAgg(fig, master=label)
canvas.get_tk_widget().pack(side="top", fill="both", expand=True)
canvas._tkcanvas.pack(side="top", fill="both", expand=True)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
back_button.pack()
try:
graph()
except:
pass
class Indefinite(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Indefinite Integrals", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
back_button = tk.Button(self, text="Back",
command=lambda: controller.show_frame("Menu"))
def integrate(expr):
return sp.latex(sp.integrate(expr))
def graph(event=None):
i = entry.get()
tmptext = integrate(i)
tmptext = "$\int$" + "$" + sp.latex(sp.sympify(i)) + "\ dx" + "=" + tmptext + "+c" + "$"
ax.clear()
if 120 <= len(tmptext) <= 122:
fs = 12
elif 122 < len(tmptext) <= 140:
fs = 11
print(len(tmptext))
else:
fs = 16
ax.text(0.05, .4, tmptext, fontsize=fs)
canvas.draw()
symplot(sp.integrate(i))
# symplot(i)
entry = tk.Entry(self, width=70)
entry.pack()
tk.Button(self, text="Integrate", command=graph).pack()
label = tk.Label(self)
label.pack()
fig = matplotlib.figure.Figure(figsize=(10, 1), dpi=80)
ax = fig.add_subplot(111)
canvas = FigureCanvasTkAgg(fig, master=label)
canvas.get_tk_widget().pack(side="top", fill="both", expand=True)
canvas._tkcanvas.pack(side="top", fill="both", expand=True)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
back_button.pack()
try:
graph()
except:
pass
class Definite(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Definite Integrals", font=controller.title_font)
label.grid(row=1, column=1, columnspan=6)
back_button = tk.Button(self, text="Back",
command=lambda: controller.show_frame("Menu"))
def integrate(expr, up, low):
x = sp.symbols('x')
return sp.latex(sp.integrate(expr, (x, low, up)))
def graph(event=None):
i = entry.get()
l = lower.get()
u = upper.get()
tmptext = integrate(i, u, l)
tmptext = "$\int_{%s}^{%s}$" % (l, u) + "$" + sp.latex(sp.sympify(i)) + "\ dx" + "=" + tmptext + "$"
ax.clear()
if 120 <= len(tmptext) <= 122:
fs = 12
elif 122 < len(tmptext) <= 140:
fs = 11
print(len(tmptext))
else:
fs = 16
ax.text(0.05, .4, tmptext, fontsize=fs)
canvas.draw()
x = sp.symbols('x')
symplot(i, (x, int(l), int(u)))
upper = tk.StringVar()
tk.Entry(self, textvariable=upper, width=3).grid(row=2, column=2, sticky='w')
tk.Label(self, text="upper").grid(row=2, column=1, sticky='e')
lower = tk.StringVar()
tk.Entry(self, textvariable=lower, width=3).grid(row=3, column=2, sticky='w')
tk.Label(self, text="lower").grid(row=3, column=1, sticky='e')
entry = tk.StringVar()
tk.Entry(self, textvariable=entry, width=50).grid(row=3, column=3)
tk.Button(self, text="Integrate", command=graph).grid(row=3, column=4, sticky='w')
label = tk.Label(self)
label.grid(row=6, column=1, columnspan=6)
fig = matplotlib.figure.Figure(figsize=(10, 1), dpi=80)
ax = fig.add_subplot(111)
canvas = FigureCanvasTkAgg(fig, master=label)
canvas.get_tk_widget().grid(row=7, column=1, columnspan=6)
canvas._tkcanvas.grid(row=8, column=1, columnspan=6)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
back_button.grid(row=9, column=2)
try:
graph()
except:
pass
app = Main()
def main():
app.title("Advanced Calculus")
app.resizable(False, False)
app.mainloop()
if __name__ == "__main__":
main()