This repository has been archived by the owner on Aug 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
238 lines (204 loc) · 9.57 KB
/
ui.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
from tkinter import *
from tkinter import messagebox
from plotter import *
from solver import *
matplotlib.use("TkAgg")
class MainWindow:
_VERSION = "6.0"
_EULER = 1
_IMPROVED_EULER = 2
_RUNGE_KUTTA = 3
_FONT = ("Consolas", 20)
def _entry(self, root: Widget) -> Entry:
entry = Entry(root, background="bisque", width=10, font=self._FONT)
entry.pack(expand=True, fill=BOTH, side=RIGHT)
return entry
def _label(self, root: Widget, text: str) -> Label:
label = Label(root, text=text, justify=RIGHT, font=self._FONT)
label.pack(expand=True, fill=BOTH)
return label
def _place_root(self):
self.root.title("DE Computational Practicum, v. " + self._VERSION)
self.root.resizable(True, True)
self.root.state('zoomed')
def _place_frames(self):
self.frames = [[Frame()] * 12 for _ in range(12)]
for i in range(12):
for j in range(12):
self.frames[i][j] = Frame(self.root, background='linen', height=30, width=30)
self.frames[i][j].grid(row=i, column=j, sticky=N + S + E + W)
self.frames[i][j].bind("<Button-1>", lambda event: self.frames[i][j].focus_set())
self.root.grid_columnconfigure(j, weight=1)
self.root.grid_rowconfigure(i, weight=1)
def _place_solution_area(self):
# 'Solutions' label
self.frames[1][1] = Frame(self.root, background="bisque2", bd=5)
self.frames[1][1].grid(row=1, column=1, sticky=N + S + E + W, columnspan=5)
self._label(self.frames[1][1], text="Solutions")
# Solutions plot frame
self.frames[2][1] = Frame(self.root, background="lightblue", bd=5)
self.frames[2][1].grid(row=2, column=1, sticky=N + S + E + W, columnspan=5, rowspan=4)
def _place_solution_plot(self):
self.solution_plotter = Plotter(self.frames[2][1])
self.solution_plotter.plot(Plotter.DEFAULT_DATA1)
self.solution_plotter.plot(Plotter.DEFAULT_DATA2)
self.solution_plotter.create_legend()
self.solution_plotter. \
widget.bind(
"<Button-1>",
lambda event: self._create_plot_in_a_new_window([Plotter.DEFAULT_DATA1,
Plotter.DEFAULT_DATA2])
)
def _place_error_area(self):
# 'Errors' label frame
self.frames[6][1] = Frame(self.root, background="bisque2", bd=5)
self.frames[6][1].grid(row=6, column=1, sticky=N + S + E + W, columnspan=5)
self.frames[6][1].grid_columnconfigure(0, weight=10)
self.frames[6][1].grid_columnconfigure(1, weight=1)
self.frames[6][1].grid_rowconfigure(0, weight=1)
self.frames[6][1].grid_rowconfigure(1, weight=1)
left = Label(self.frames[6][1], text="Errors", font=self._FONT)
right = Frame(self.frames[6][1], bg="bisque2")
left.grid(row=0, column=0, sticky=N + S + E + W, rowspan=2)
right.grid(row=0, column=1, sticky=N + S + E + W, rowspan=2)
# switch button
self.switch = Button(right,
text="Switch to:\ntotal error",
font=("Consolas", 8),
bg="#EAB4A8",
bd=5, command=self._switch_error)
self.switch.pack(expand=True, fill=BOTH)
# Errors plot frame
self.frames[7][1] = Frame(self.root, background="lightblue", bd=5)
self.frames[7][1].grid(row=7, column=1, sticky=N + S + E + W, columnspan=5, rowspan=4)
def _place_error_plot(self):
self.error_plotter = Plotter(self.frames[7][1])
self.error_plotter.plot(Plotter.DEFAULT_DATA1)
self.error_plotter.create_legend()
self.error_plotter. \
widget.bind(
"<Button-1>",
lambda event: self._create_plot_in_a_new_window([Plotter.DEFAULT_DATA1])
)
def _place_input_area(self):
names = ["x0", "y0", "X", "M", "N"]
for i in range(1, 1 + len(names)):
self._label(self.frames[i][7], names[i - 1])
self.frames[i][8] = Frame(self.root, background="bisque", bd=5)
self.frames[i][8].grid(row=i, column=8, sticky=N + S + E + W, columnspan=3)
self.__setattr__(names[i - 1] + "_entry", self._entry(self.frames[i][8]))
# setting input fields to default values
def _init_input_area(self):
self.x0_entry.insert("0", "0")
self.y0_entry.insert("0", "sqrt(1/2)")
self.X_entry.insert("0", "3")
self.N_entry.insert("0", "200")
self.M_entry.insert("0", "20")
def _place_radiobox(self):
self.method_selected = IntVar()
method_names = ["Euler", "Improved Euler", "Runge-Kutta"]
method_values = [self._EULER, self._IMPROVED_EULER, self._RUNGE_KUTTA]
for i in range(7, 10):
self.frames[i][8] = Frame(self.root, background="white", bd=5)
self.frames[i][8].grid(row=i, column=7,
sticky=N + S + E + W,
columnspan=4)
Radiobutton(self.frames[i][8],
text=method_names[i - 7],
variable=self.method_selected,
value=method_values[i - 7],
background='white',
font=self._FONT).pack(expand=True,
anchor=W)
self.method_selected.set(self._EULER)
def _place_button(self):
self.apply = Button(self.frames[10][10], text="Apply", command=self._solve, font=self._FONT)
self.apply.pack(expand=True, fill=BOTH)
def __init__(self, root: Tk):
self.root = root
self._place_root()
self._place_frames()
self._place_solution_area()
self._place_solution_plot()
self._place_error_area()
self._place_error_plot()
self._place_input_area()
self._init_input_area()
self._place_radiobox()
self._place_button()
# initializing attributes for error type switching
self.error_flag = 1
self.local_error = Plotter.DEFAULT_DATA1
self.total_error = Plotter.DEFAULT_DATA2
self.current_error = self.local_error
# binding up and down arrow keys for scrolling through methods
self.root.bind("<Up>", lambda event: self._move_up())
self.root.bind("<Down>", lambda event: self._move_down())
self.root.bind("<Right>", lambda event: self._switch_error())
self.root.bind("<Left>", lambda event: self._switch_error())
# binding 'Enter' key to 'Apply' button action
self.root.bind("<Return>", lambda event: self.apply.invoke())
# commands associated with widgets
def _switch_error(self):
if str(self.root.focus_get().__class__) != "<class 'tkinter.Entry'>":
names = ["local", "total"]
errors = [self.total_error, self.local_error]
self.error_flag = ~self.error_flag
self.switch.configure(text=f"Switch to:\n{names[self.error_flag]} error")
self.current_error = errors[self.error_flag]
self.error_plotter.redraw([self.current_error])
def _move_down(self):
value = self.method_selected.get()
self.method_selected.set(value + 1 if value < 3 else 1)
def _move_up(self):
value = self.method_selected.get()
self.method_selected.set(value - 1 if value > 1 else 3)
def _gather_data(self):
names = ["x0", "y0", "X", "N", "M"]
data = dict()
for i in names:
try:
data[i] = float(eval(getattr(self, i + "_entry").get()))
except:
messagebox.showerror("Input Error", "x0, y0, X or N have incorrect format!")
break
else:
data['method'] = self.method_selected.get()
return data
return dict()
def _solve(self):
input_data = self._gather_data()
try:
solver = Solver(input_data)
exact_solution = solver.solve_exact()
numerical_solution_data = solver.solve_numerical()
except NoDataException:
pass
except SolverException as exception:
messagebox.showerror("Error", exception.strerror)
else:
solutions = [exact_solution, numerical_solution_data[0]]
self.local_error = numerical_solution_data[1]
self.total_error = numerical_solution_data[2]
errors = [self.total_error, self.local_error]
self.solution_plotter.redraw(solutions)
self.error_plotter.redraw([errors[self.error_flag]])
# rebinding the left mouse button to redraw the graph in a new window for the given data
self._rebind(solutions, errors)
def _create_plot_in_a_new_window(self, datasets: list):
new_window = Toplevel(self.root)
new_window.title("Plot")
new_window.state("zoomed")
plotter = Plotter(new_window)
for i in range(len(datasets)):
plotter.plot(Plotter.EMPTY_DATA)
plotter.create_legend()
plotter.redraw(datasets)
toolbar = NavigationToolbar2Tk(plotter.canvas, new_window)
toolbar.pack()
def _rebind(self, solutions, errors):
self.switch.focus_set()
self.solution_plotter.widget.bind("<Button-1>",
lambda event: self._create_plot_in_a_new_window(solutions))
self.error_plotter.widget.bind("<Button-1>",
lambda event: self._create_plot_in_a_new_window([errors[self.error_flag]]))