forked from krasnikovra/mini_lab_1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
260 lines (212 loc) · 9.95 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
import json
import matplotlib
import numexpr as ne
import numpy as np
from functools import partial
from tkinter import *
from tkinter.filedialog import asksaveasfile
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2Tk)
matplotlib.use('TkAgg')
# class for entries storage (класс для хранения текстовых полей)
class Entries:
def __init__(self):
self.entries_list = []
self.parent_window = None
def set_parent_window(self, parent_window):
self.parent_window = parent_window
# adding of new entry (добавление нового текстового поля)
def add_entry(self):
new_entry = Entry(self.parent_window)
new_entry.icursor(0)
new_entry.focus()
new_entry.pack()
plot_button = self.parent_window.get_button_by_name('plot')
if plot_button:
plot_button.pack_forget()
self.parent_window.add_button('plot', 'Plot', 'plot', hot_key='<Return>')
self.entries_list.append(new_entry)
# class for plotting (класс для построения графиков)
class Plotter:
def __init__(self, x_min=-20, x_max=20, dx=0.01):
self.x_min = x_min
self.x_max = x_max
self.dx = dx
self._last_plotted_list_of_function = None
self._last_plotted_figure = None
self.parent_window = None
def set_parent_window(self, parent_window):
self.parent_window = parent_window
# plotting of graphics (построение графиков функций)
def plot(self, list_of_function, title='Графики функций', x_label='x', y_label='y', need_legend=True):
fig = plt.figure()
x = np.arange(self.x_min, self.x_max, self.dx)
new_funcs = [f if 'x' in f else 'x/x * ({})'.format(f) for f in list_of_function]
ax = fig.add_subplot(1, 1, 1)
for func in new_funcs:
ax.plot(x, ne.evaluate(func), linewidth=1.5)
fig.suptitle(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
if need_legend:
plt.legend(list_of_function)
self._last_plotted_list_of_function = list_of_function
self._last_plotted_figure = fig
return fig
# class for commands storage (класс для хранения команд)
class Commands:
class State:
def __init__(self):
self.list_of_function = []
def save_state(self):
tmp_dict = {'list_of_function': self.list_of_function}
file_out = asksaveasfile(defaultextension=".json")
if file_out is not None:
json.dump(tmp_dict, file_out)
return self
def reset_state(self):
self.list_of_function = []
def __init__(self):
self.command_dict = {}
self.__figure_canvas = None
self.__navigation_toolbar = None
self._state = Commands.State()
self.__empty_entry_counter = 0
self.parent_window = None
def set_parent_window(self, parent_window):
self.parent_window = parent_window
def add_command(self, name, command):
self.command_dict[name] = command
def get_command_by_name(self, command_name):
return self.command_dict[command_name]
def __forget_canvas(self):
if self.__figure_canvas is not None:
self.__figure_canvas.get_tk_widget().pack_forget()
def __forget_navigation(self):
if self.__navigation_toolbar is not None:
self.__navigation_toolbar.pack_forget()
def plot(self, *args, **kwargs):
def is_not_blank(s):
return bool(s and not s.isspace())
self._state.reset_state()
list_of_function = []
for entry in self.parent_window.entries.entries_list:
get_func_str = entry.get()
self._state.list_of_function.append(get_func_str)
if is_not_blank(get_func_str):
list_of_function.append(get_func_str)
else:
if self.__empty_entry_counter == 0:
mw = ModalWindow(self.parent_window, title='Пустая строка', labeltext='Это пример модального окна, '
'возникающий, если ты ввел '
'пустую '
'строку. С этим ничего '
'делать не нужно. '
'Просто нажми OK :)')
ok_button = Button(master=mw.top, text='OK', command=mw.cancel)
mw.add_button(ok_button)
self.__empty_entry_counter = 1
self.__empty_entry_counter = 0
figure = self.parent_window.plotter.plot(list_of_function)
self._state.figure = figure
self.__forget_canvas()
self.__figure_canvas = FigureCanvasTkAgg(figure, self.parent_window)
self.__forget_navigation()
self.__navigation_toolbar = NavigationToolbar2Tk(self.__figure_canvas, self.parent_window)
self.__figure_canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
plot_button = self.parent_window.get_button_by_name('plot')
if plot_button:
plot_button.pack_forget()
def add_func(self, *args, **kwargs):
self.__forget_canvas()
self.__forget_navigation()
self.parent_window.entries.add_entry()
def save_as(self):
self._state.save_state()
return self
# class for buttons storage (класс для хранения кнопок)
class Buttons:
def __init__(self):
self.buttons = {}
self.parent_window = None
def set_parent_window(self, parent_window):
self.parent_window = parent_window
def add_button(self, name, text, command):
new_button = Button(master=self.parent_window, text=text, command=command)
self.buttons[name] = new_button
return new_button
def delete_button(self, name):
button = self.buttons.get(name)
if button:
button.pack_forget()
# class for generate modal windows (класс для генерации модальных окон)
class ModalWindow:
def __init__(self, parent, title, labeltext=''):
self.buttons = []
self.top = Toplevel(parent)
self.top.transient(parent)
self.top.grab_set()
if len(title) > 0:
self.top.title(title)
if len(labeltext) == 0:
labeltext = 'Default text'
Label(self.top, text=labeltext).pack()
def add_button(self, button):
self.buttons.append(button)
button.pack(pady=5)
def cancel(self):
self.top.destroy()
# app class (класс приложения)
class App(Tk):
def __init__(self, buttons, plotter, commands, entries):
super().__init__()
self.buttons = buttons
self.plotter = plotter
self.commands = commands
self.entries = entries
self.entries.set_parent_window(self)
self.plotter.set_parent_window(self)
self.commands.set_parent_window(self)
self.buttons.set_parent_window(self)
def add_button(self, name, text, command_name, *args, **kwargs):
hot_key = kwargs.get('hot_key')
if hot_key:
kwargs.pop('hot_key')
callback = partial(self.commands.get_command_by_name(command_name), *args, **kwargs)
new_button = self.buttons.add_button(name=name, text=text, command=callback)
if hot_key:
self.bind(hot_key, callback)
new_button.pack(fill=BOTH)
def get_button_by_name(self, name):
return self.buttons.buttons.get(name)
def create_menu(self):
menu = Menu(self)
self.config(menu=menu)
file_menu = Menu(menu)
file_menu.add_command(label="Save as...", command=self.commands.get_command_by_name('save_as'))
menu.add_cascade(label="File", menu=file_menu)
if __name__ == "__main__":
# init buttons (создаем кнопки)
buttons_main = Buttons()
# init plotter (создаем отрисовщик графиков)
plotter_main = Plotter()
# init commands for executing on buttons or hot keys press
# (создаем команды, которые выполняются при нажатии кнопок или горячих клавиш)
commands_main = Commands()
# init entries (создаем текстовые поля)
entries_main = Entries()
# command's registration (регистрация команд)
commands_main.add_command('plot', commands_main.plot)
commands_main.add_command('add_func', commands_main.add_func)
commands_main.add_command('save_as', commands_main.save_as)
# init app (создаем экземпляр приложения)
app = App(buttons_main, plotter_main, commands_main, entries_main)
# init add func button (добавляем кнопку добавления новой функции)
app.add_button('add_func', 'Добавить функцию', 'add_func', hot_key='<Control-a>')
# init first entry (создаем первое поле ввода)
entries_main.add_entry()
app.create_menu()
# добавил комментарий для коммита
# application launch (запуск "вечного" цикла приложеня)
app.mainloop()