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
|
import tkinter as tk
from tkinter import filedialog as fd
from tkinter import ttk
class PlotType:
USER = 1
PRESET = 2
POINT = 3
class PresetPlot:
LINE = 0
PARABOLA = 1
OPTIONS = ["ax + b = 0", "ax^2 + bx + c = 0"]
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, preset_type):
super().__init__(master)
self.master = master
self.preset_type = preset_type
self.create_widgets()
def create_widgets(self):
self.options = ttk.OptionMenu(
self,
self.preset_type,
PresetPlot.OPTIONS[0],
*PresetPlot.OPTIONS
)
self.options.grid(row=0, column=0, columnspan=1, rowspan=1, sticky=tk.NSEW)
class UserPlotFrame(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.master = master
self.create_widgets()
def create_widgets(self):
pass
class PointPlotFrame(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.master = master
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, sticky=tk.NSEW)
self.filepath = tk.Entry(self)
self.filepath.grid(row=1, column=0)
def open_file(self):
a = tk.filedialog.askopenfile()
# self.filepath.set(a.name)
# print(self.filepath.get())
self.filepath.delete(0, tk.END)
self.filepath.insert(0, a.name)
print(a)
class App(tk.Frame):
def __init__(self, master):
super().__init__(master)
# self.geometry("400x250")
self.grid(row=0, column=0, columnspan=2, rowspan=4)
self.grid_columnconfigure(0, weight=1, uniform="foo")
self.grid_columnconfigure(1, weight=1, uniform="foo")
self.create_widgets()
def create_widgets(self):
# Создаём фреймы с настройкой выбранного типа графика
self.preset_type = tk.IntVar()
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)
self.preset_plot_frame = PresetPlotFrame(self.right_frame, self.preset_type)
self.user_plot_frame = UserPlotFrame(self.right_frame)
self.point_plot_frame = PointPlotFrame(self.right_frame)
# self.preset_plot_frame.pack(side=tk.RIGHT)
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, columnspan=2, padx=10, pady=10, sticky=tk.W)
self.submit_btn.grid(row=3, column=0, 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.grid(column=0, row=0, padx=10, pady=10)
else:
widget.grid_forget()
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
|