Skip to content

Commit 328a7ae

Browse files
authored
Add files via upload
1 parent fb3056d commit 328a7ae

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

GUI/Quadratic-Equation-Solver/quad.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
5+
class Quadratic:
6+
"""Representing a quadratic expression in the form of ax² + bx + c"""
7+
8+
def __init__(self,a : float, b : float, c : float) -> None:
9+
# A class is created succesfully if the arguments of the class are either float or int
10+
if (type(a) == type(0.1) or type(a) == type(1)) and (type(b) == type(0.1) or type(b) == type(1)) and (type(c) == type(0.1) or type(c) == type(1)):
11+
self.a = a
12+
self.b = b
13+
self.c = c
14+
else:
15+
raise ValueError("Argument must be of type int or float")
16+
17+
def __repr__(self) -> str:
18+
return "Quad( {0}x² + {1}x + {2} )".format(self.a,self.b,self.c)
19+
20+
def solveQuad(self) -> tuple[float,float]:
21+
"""Solving the expression assuming it is equal to 0 returns a tuple of 2 values"""
22+
determinant = ((self.b ** 2) - (4 * self.a * self.c)) ** 0.5
23+
firstSolution = ((-1 * self.b) + determinant) / (2 * self.a)
24+
secondSolution = ((-1 * self.b) - determinant) / (2 * self.a)
25+
return (firstSolution,secondSolution)
26+
27+
def evaluate(self, x):
28+
"""Substitute x for a value and return a string to be called by the eval function"""
29+
solution = ((self.a * (x ** 2)) + (self.b * x) + self.c)
30+
return solution
31+
32+
def drawFigure(self):
33+
"""Draws the quadratic graph and returns a matplotlib figure"""
34+
x_axis = np.linspace(-10,10,50)
35+
y_axis = self.evaluate(x_axis)
36+
37+
figure, axes = plt.subplots()
38+
39+
axes.plot(x_axis,y_axis, linewidth = 1)
40+
axes.grid(visible = True)
41+
axes.set_title(self)
42+
axes.xaxis.set_minor_formatter(str(x_axis))
43+
return figure
44+

GUI/Quadratic-Equation-Solver/ui.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import ttkbootstrap as tb
2+
from ttkbootstrap.constants import *
3+
from tkinter import *
4+
from ttkbootstrap.scrolled import ScrolledFrame
5+
from quad import Quadratic
6+
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
7+
from ttkbootstrap.dialogs import Messagebox
8+
9+
# Most variables are named a,b,c according to ax² + bx + c in a quadratic equation
10+
11+
12+
def plot():
13+
14+
# Collect all inputs
15+
a_entryInput = a_entry.get()
16+
b_entryInput = b_entry.get()
17+
c_entryInput = c_entry.get()
18+
19+
try:
20+
a = float(a_entryInput)
21+
b = float(b_entryInput)
22+
c = float(c_entryInput)
23+
eqn = Quadratic(a,b,c)
24+
25+
#--------------------------------------------------------------------------
26+
# Background Graph Frame
27+
graph_frame = ScrolledFrame(root, width = 800, height = 500)
28+
graph_frame.grid(row = 1, column = 0,pady = 10)
29+
30+
fig = eqn.drawFigure()
31+
32+
canvas = FigureCanvasTkAgg(figure = fig, master = graph_frame)
33+
canvas.draw()
34+
canvas.get_tk_widget().pack()
35+
36+
toolbar = NavigationToolbar2Tk(canvas, graph_frame)
37+
toolbar.update()
38+
39+
canvas.get_tk_widget().pack()
40+
solution_label.config(text = "Solution : x₁ = {0}, x₂ = {1}".format(eqn.solveQuad()[0], eqn.solveQuad()[1]))
41+
42+
except:
43+
Messagebox.show_error(title = "Error", message = "User entered wrong value")
44+
45+
46+
47+
# Base window widget
48+
root = tb.Window(themename="vapor")
49+
root.geometry("720x720")
50+
root.title("Quadratic Equation Solver")
51+
52+
# Font data
53+
font = ("Nunito", 12)
54+
55+
# Frame containig the entry for the three arguments
56+
top_frame = tb.Frame(root)
57+
top_frame.grid(row = 0, column = 0,padx = 10, pady = 20)
58+
59+
# Entry for the three arguments
60+
a_frame = tb.Frame(top_frame)
61+
a_frame.grid(row = 0, column = 0, padx=5)
62+
63+
a_label = tb.Label(a_frame, text= "a =", font = font)
64+
a_label.grid(row = 0, column = 0)
65+
66+
a_entry = tb.Entry(a_frame, width = 30, font = font)
67+
a_entry.grid(row = 0, column = 1)
68+
69+
b_frame = tb.Frame(top_frame)
70+
b_frame.grid(row = 0, column = 1, padx=5)
71+
72+
b_label = tb.Label(b_frame, text = "b =", font = font)
73+
b_label.grid(row = 0, column = 0)
74+
75+
b_entry = tb.Entry(b_frame, width = 30, font = font)
76+
b_entry.grid(row = 0, column = 1)
77+
78+
c_frame = tb.Frame(top_frame)
79+
c_frame.grid(row = 0, column = 2, padx=5)
80+
81+
c_label = tb.Label(c_frame, text = "c =", font = font)
82+
c_label.grid(row = 0, column = 0)
83+
84+
c_entry = tb.Entry(c_frame, width = 30, font = font)
85+
c_entry.grid(row = 0, column = 1)
86+
87+
88+
# Button to plot the matplotlib graph
89+
plot_button = tb.Button(top_frame, width = 70, text = "Plot", command = plot)
90+
plot_button.grid(row = 1, column = 1, pady = 15)
91+
92+
# Label containing the solution to the equation
93+
solution_label = tb.Label(root,font = (font[0], 15), text = "")
94+
solution_label.grid(row = 2, column = 0, pady = 10)
95+
96+
root.mainloop()

0 commit comments

Comments
 (0)