diff options
| author | Andrew Guschin <saintruler@gmail.com> | 2021-04-05 15:40:54 +0400 |
|---|---|---|
| committer | Andrew Guschin <saintruler@gmail.com> | 2021-04-05 15:40:54 +0400 |
| commit | cc45ab13adf5f06ae3fc7c2334b63ef8f8c79a23 (patch) | |
| tree | 74176736aac28f95d3cd2b6d7ca33d9da404be2d | |
| parent | d7228cd0a9c05b01b6c19cd7ac171aa44b2fd5e8 (diff) | |
Разместил виджеты по сетке
| -rw-r--r-- | python-graphics/main.py | 153 |
1 files changed, 82 insertions, 71 deletions
diff --git a/python-graphics/main.py b/python-graphics/main.py index 9afed98..300a10a 100644 --- a/python-graphics/main.py +++ b/python-graphics/main.py @@ -1,5 +1,6 @@ import tkinter as tk from tkinter import filedialog as fd +from tkinter import ttk class PlotType: @@ -23,50 +24,38 @@ class PlotTypeFrame(tk.Frame): self.callback = callback self.create_widgets() - self.set_style() - - def set_style(self): - self.font = (None, "15") - # self.font["size"] = 17 - - self.bg = "#ffffff" - self["bg"] = self.bg - - self.preset_plot["bg"] = self.bg - self.preset_plot["font"] = self.font - self.preset_plot["bd"] = 0 - - self.user_plot["bg"] = self.bg - self.user_plot["font"] = self.font - self.user_plot["bd"] = 0 - - self.point_plot["bg"] = self.bg - self.point_plot["font"] = self.font - self.point_plot["bd"] = 0 def create_widgets(self): - self.preset_plot = tk.Radiobutton(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) + self.preset_plot.grid(row=0, column=0, sticky=tk.W) + # self.preset_plot.pack(side=tk.TOP, expand=True) - self.user_plot = tk.Radiobutton(self) + 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) + self.user_plot.grid(row=1, column=0, sticky=tk.W) + # self.user_plot.pack(side=tk.TOP, sticky=tk.W) - self.point_plot = tk.Radiobutton(self) + 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) + self.point_plot.grid(row=2, column=0, sticky=tk.W) - self.preset_plot.select() + self.preset_plot.invoke() + + # self.frame.grid(row=0, column=0, rowspan=3, columnspan=1, ipadx=10) class PresetPlotFrame(tk.Frame): @@ -78,16 +67,13 @@ class PresetPlotFrame(tk.Frame): self.create_widgets() def create_widgets(self): - self.label = tk.Label(self) - self.label["text"] = "Предустановленный" - self.label.grid(row=0, column=0) - - self.options = tk.OptionMenu( + self.options = ttk.OptionMenu( self, self.preset_type, + PresetPlot.OPTIONS[0], *PresetPlot.OPTIONS ) - self.options.grid(row=1, column=0) + self.options.grid(row=0, column=0, columnspan=1, rowspan=1, sticky=tk.NSEW) class UserPlotFrame(tk.Frame): @@ -98,9 +84,7 @@ class UserPlotFrame(tk.Frame): self.create_widgets() def create_widgets(self): - self.label = tk.Label(self) - self.label["text"] = "Пользовательский" - self.label.grid(row=0, column=0) + pass class PointPlotFrame(tk.Frame): @@ -111,66 +95,93 @@ class PointPlotFrame(tk.Frame): self.create_widgets() def create_widgets(self): - self.label = tk.Label(self) - self.label["text"] = "Поточечный" - self.label.grid(row=0, column=0) - self.choose_file = tk.Button(self) self.choose_file["text"] = "Выбрать файл" self.choose_file["command"] = self.open_file - self.choose_file.grid(row=1, column=0) + self.choose_file.grid(row=0, column=0, sticky=tk.NSEW) - self.filepath = tk.Label(self) - self.filepath.grid(row=1, column=1) + self.filepath = tk.Entry(self) + self.filepath.grid(row=1, column=0) def open_file(self): a = tk.filedialog.askopenfile() - self.filepath["text"] = a.name + # 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.Tk): - def __init__(self): - super().__init__() - self.geometry("400x250") +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.plot_type = tk.IntVar() + # Создаём фреймы с настройкой выбранного типа графика 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.plot_type_frame = PlotTypeFrame( - self, + 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.plot_type_frame.pack(side=tk.LEFT) + self.w_plot_type.grid(row=0, column=0, rowspan=3, padx=3, pady=3) - # Создаём фреймы с настройкой выбранного типа графика - self.preset_plot_frame = PresetPlotFrame(self, self.preset_type) - self.user_plot_frame = UserPlotFrame(self) - self.point_plot_frame = PointPlotFrame(self) + ############################# + + self.change_plot_type() - self.preset_plot_frame.pack(side=tk.RIGHT) + 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): - if self.plot_type.get() == PlotType.PRESET: - self.preset_plot_frame.pack(side=tk.RIGHT) - self.user_plot_frame.pack_forget() - self.point_plot_frame.pack_forget() - elif self.plot_type.get() == PlotType.USER: - self.preset_plot_frame.pack_forget() - self.user_plot_frame.pack(side=tk.RIGHT) - self.point_plot_frame.pack_forget() - elif self.plot_type.get() == PlotType.POINT: - self.preset_plot_frame.pack_forget() - self.user_plot_frame.pack_forget() - self.point_plot_frame.pack(side=tk.RIGHT) + 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__": - app = App() - app.mainloop() + root = tk.Tk() + app = App(root) + root.mainloop() |