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