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
261
262
263
264
265
|
import tkinter as tk
from tkinter import filedialog as fd
from tkinter import ttk
class PlotType:
USER = 1
PRESET = 2
POINT = 3
class PresetType:
LINE = "ax + b = 0"
PARABOLA = "ax^2 + bx + c = 0"
@classmethod
def options(cls):
return [cls.LINE, cls.PARABOLA]
class PlotTypeFrame(tk.Frame):
def __init__(self, master, plot_type_var, callback):
super().__init__(master)
self.master = master
self.plot_type = plot_type_var
self.callback = callback
self.create_widgets()
def create_widgets(self):
# self.frame = ttk.LabelFrame(self)
# self.frame["text"] = "Тип графика"
self.frame = self
self.preset_plot = ttk.Radiobutton(self.frame)
self.preset_plot["variable"] = self.plot_type
self.preset_plot["text"] = "Предустановленный"
self.preset_plot["value"] = PlotType.PRESET
self.preset_plot["command"] = self.callback
self.preset_plot.grid(row=0, column=0, sticky=tk.W)
# self.preset_plot.pack(side=tk.TOP, expand=True)
self.user_plot = ttk.Radiobutton(self.frame)
self.user_plot["variable"] = self.plot_type
self.user_plot["text"] = "Пользовательский"
self.user_plot["value"] = PlotType.USER
self.user_plot["command"] = self.callback
self.user_plot.grid(row=1, column=0, sticky=tk.W)
# self.user_plot.pack(side=tk.TOP, sticky=tk.W)
self.point_plot = ttk.Radiobutton(self.frame)
self.point_plot["variable"] = self.plot_type
self.point_plot["text"] = "По точкам"
self.point_plot["value"] = PlotType.POINT
self.point_plot["command"] = self.callback
self.point_plot.grid(row=2, column=0, sticky=tk.W)
self.preset_plot.invoke()
# self.frame.grid(row=0, column=0, rowspan=3, columnspan=1, ipadx=10)
class PresetPlotFrame(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.master = master
self.create_widgets()
def create_widgets(self):
self.preset_type = tk.StringVar()
self.options = ttk.OptionMenu(
self,
self.preset_type,
PresetType.options()[0],
*PresetType.options(),
)
self.variables = {}
la = tk.Label(self)
la["text"] = "a: "
a = tk.Entry(self)
lb = tk.Label(self)
lb["text"] = "b: "
b = tk.Entry(self)
lc = tk.Label(self)
lc["text"] = "c: "
c = tk.Entry(self)
self.variables[PresetType.LINE] = [(la, a), (lb, b)]
self.variables[PresetType.PARABOLA] = [(la, a), (lb, b), (lc, c)]
self.variables[None] = [(la, a), (lb, b), (lc, c)]
self.options.grid(row=0, column=0, columnspan=2, rowspan=1, sticky=tk.NSEW)
self.preset_type.trace("w", self.type_changed)
def type_changed(self, *args):
new_type = self.preset_type.get()
if new_type == PresetType.LINE:
print("line")
elif new_type == PresetType.PARABOLA:
print("parabola")
for (label, var) in self.variables[None]:
label.grid_forget()
var.grid_forget()
for i, (label, var) in enumerate(self.variables[new_type], 1):
label.grid(row=i, column=0)
var.grid(row=i, column=1)
def grid_size(self):
return (4, 2)
def show(self):
self.grid(row=0, column=0, columnspan=2, rowspan=4, sticky=tk.NSEW)
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=4)
class UserPlotFrame(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.master = master
self.create_widgets()
def create_widgets(self):
pass
def grid_size(self):
return (2, 1)
def show(self):
self.grid(row=0, column=0, columnspan=1, rowspan=2, sticky=tk.NSEW)
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=1)
self.grid_rowconfigure(1, weight=1)
class PointPlotFrame(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.master = master
self.show()
self.create_widgets()
def create_widgets(self):
self.choose_file = tk.Button(self)
self.choose_file["text"] = "Выбрать файл"
self.choose_file["command"] = self.open_file
self.choose_file.grid(row=0, column=0, columnspan=1, rowspan=1, padx=3, pady=3, sticky=tk.NSEW)
self.filepath = tk.Entry(self)
self.filepath.grid(row=1, column=0, sticky=tk.NSEW)
def open_file(self):
a = tk.filedialog.askopenfile()
self.filepath.delete(0, tk.END)
self.filepath.insert(0, a.name)
print(a)
def grid_size(self):
return (2, 1)
def show(self):
gr, gc = self.grid_size()
self.grid(row=0, column=0, columnspan=gc, rowspan=gr, sticky=tk.NSEW)
grid_conf(self, gr, gc)
class App(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.grid(row=0, column=0, columnspan=2, rowspan=4, sticky=tk.NSEW)
self.create_widgets()
def create_widgets(self):
# Создаём фреймы с настройкой выбранного типа графика
self.cfg_pt = {
"row": 0, "column": 1,
"rowspan": 3, "columnspan": 1,
"padx": 10, "pady": 10,
"sticky": tk.NSEW
}
self.right_frame = ttk.LabelFrame(self)
self.right_frame["text"] = "Настройка графика"
self.right_frame.grid(**self.cfg_pt)
grid_conf(self.right_frame, 3, 1)
self.preset_plot_frame = PresetPlotFrame(self.right_frame)
self.user_plot_frame = UserPlotFrame(self.right_frame)
self.point_plot_frame = PointPlotFrame(self.right_frame)
self.pw = {
PlotType.PRESET: self.preset_plot_frame,
PlotType.USER: self.user_plot_frame,
PlotType.POINT: self.point_plot_frame,
}
# Фрейм с выбором типа графика
self.left_frame = ttk.LabelFrame(self)
self.left_frame["text"] = "Тип графика"
self.left_frame.grid(row=0, column=0, rowspan=3, columnspan=1, padx=10, pady=10, sticky=tk.NSEW)
self.plot_type = tk.IntVar()
self.w_plot_type = PlotTypeFrame(
self.left_frame,
self.plot_type,
self.change_plot_type
)
self.w_plot_type.grid(row=0, column=0, rowspan=3, padx=3, pady=3)
#############################
self.change_plot_type()
self.submit_btn = tk.Button(self)
self.submit_btn["text"] = "Построить график"
self.submit_btn.grid(row=3, column=0, rowspan=1, columnspan=2, padx=10, pady=10, sticky=tk.NSEW)
def change_plot_type(self):
cur_type = self.plot_type.get()
for ptype, widget in self.pw.items():
if cur_type == ptype:
widget.show()
# gr, gc = widget.grid_size()
# self.right_frame.grid_forget()
# self.right_frame.grid(
# row=0, column=1,
# rowspan=gr, columnspan=gc,
# padx=10, pady=10,
# sticky=tk.NSEW
# )
# grid_conf(self.right_frame, gr, gc)
else:
widget.grid_forget()
def grid_conf(obj, rows, cols, uniform=False):
for i in range(rows):
obj.grid_rowconfigure(i, weight=1, uniform=uniform)
# tk.Grid.rowconfigure(obj, i, weight=1, uniform=uniform)
for i in range(cols):
obj.grid_columnconfigure(i, weight=1, uniform=uniform)
# tk.Grid.columnconfigure(obj, i, weight=1, uniform=uniform)
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
grid_conf(app, 4, 2)
grid_conf(root, 4, 2)
root.mainloop()
|