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