-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
185 lines (145 loc) · 6.75 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
#!/usr/bin/env python3
import os.path
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter.ttk import Combobox
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import scipy.interpolate as interp
import numpy as np
class Application(tk.Tk):
name = "TkGraf"
title_ = "TkGraf"
def __init__(self):
super().__init__(className=self.name)
self.geometry("480x480")
self.title(self.title_)
self.bind("<Escape>", self.quit)
self.fileNameVar = tk.StringVar()
self.dataOrientVar = tk.StringVar(value="row")
self.labelVar = tk.StringVar(value="")
self.axisXVar = tk.StringVar(value="")
self.axisYVar = tk.StringVar(value="")
self.interpolationVar = tk.StringVar(value="")
self.lineVar = tk.StringVar(value="-")
self.markerVar = tk.StringVar(value=None)
self.colorVar = tk.StringVar(value="red")
self.master_frame = tk.LabelFrame(self, text=self.name, padx=5, pady=5)
self.file_frame = tk.LabelFrame(self.master_frame, text="Soubor")
self.option_frame = tk.LabelFrame(self.master_frame, text="Možnosti")
# FIXME master_frame doesn't cover entire app
self.master_frame.grid(padx=5, pady=5)
self.file_frame.grid(row=0, sticky="we")
self.option_frame.grid(row=1, sticky="we")
# FILE FRAME
self.file_name_entry = tk.Entry(
self.file_frame, textvariable=self.fileNameVar, width=40)
self.file_button = tk.Button(
self.file_frame, text="...", command=self.set_file)
self.file_data_row = tk.Radiobutton(
self.file_frame, text="Data v řádcích", variable=self.dataOrientVar, value="row")
self.file_data_col = tk.Radiobutton(
self.file_frame, text="Data ve sloupcích", variable=self.dataOrientVar, value="col")
self.plot_button = tk.Button(
self.option_frame, text="PLOT", command=self.plot)
self.file_name_entry.grid(row=0, column=0, sticky="e")
self.file_button.grid(row=0, column=1, sticky="w")
self.file_data_col.grid(row=1, column=0, sticky="w")
self.file_data_row.grid(row=2, column=0, sticky="w")
self.plot_button.grid(row=8)
# OPTION FRAME
self.option_label = tk.Entry(
self.option_frame, textvariable=self.labelVar)
self.label_label = tk.Label(self.option_frame, text="Nadpis: ")
self.axis_x_label = tk.Label(self.option_frame, text="Popis osy x: ")
self.axis_y_label = tk.Label(self.option_frame, text="Popis osy y: ")
self.line_label = tk.Label(self.option_frame, text="Druh čáry: ")
self.color_label = tk.Label(self.option_frame, text="Barva čáry: ")
self.marker_label = tk.Label(self.option_frame, text="Druh bodu: ")
self.option_axis_x = tk.Entry(
self.option_frame, textvariable=self.axisXVar)
self.option_axis_y = tk.Entry(
self.option_frame, textvariable=self.axisYVar)
self.line_Cbox = Combobox(self.option_frame, textvariable=self.lineVar)
self.color_Cbox = Combobox(
self.option_frame, textvariable=self.colorVar)
self.interpolation_Cbox = Combobox(
self.option_frame, textvariable=self.interpolationVar)
self.interpolation_label = tk.Label(
self.option_frame, text="Druh interpolace: ")
self.color_Cbox.bind("<<ComboboxSelected>>", self.set_canvas_color)
self.color_canvas = tk.Canvas(
self.option_frame, height=20, bg="red", bd=0)
self.interpolation_Cbox['values'] = ["","CubicSpline"]
self.line_Cbox['values'] = list(plt.Line2D.lineStyles.keys())[:4]
self.color_Cbox['values'] = list(mcolors.CSS4_COLORS.keys())
self.marker_Cbox = Combobox(
self.option_frame, textvariable=self.markerVar)
self.marker_Cbox['values'] = list(plt.Line2D.markers.keys())[:-16]
self.option_label.grid(row=0, column=1, sticky="e", pady=5)
self.label_label.grid(row=0, column=0, sticky="w")
self.axis_x_label.grid(row=1, column=0, sticky="w")
self.axis_y_label.grid(row=2, column=0, sticky="w")
self.interpolation_label.grid(row=3, column=0, sticky="w")
self.line_label.grid(row=4, column=0, sticky="w")
self.marker_label.grid(row=5, column=0, sticky="w")
self.color_label.grid(row=6, column=0, sticky="w")
self.option_axis_x.grid(row=1, column=1, sticky="e", pady=2)
self.option_axis_y.grid(row=2, column=1, sticky="e", pady=2)
self.interpolation_Cbox.grid(row=3, column=1, sticky="e", pady=2)
self.line_Cbox.grid(row=4, column=1, sticky="e", pady=2)
self.marker_Cbox.grid(row=5, column=1, sticky="e", pady=2)
self.color_Cbox.grid(row=6, column=1, sticky="e", pady=2)
self.color_canvas.grid(row=7, columnspan=2,
sticky="we", padx=20, pady=5)
def set_file(self, event=None):
file = filedialog.askopenfilename()
self.fileNameVar.set(file)
def get_file(self, event=None):
file = self.fileNameVar.get()
return file
def set_canvas_color(self, event=None):
self.color_canvas.configure(
bg=mcolors.CSS4_COLORS[self.colorVar.get()])
def read_data_row(self, file, event=None):
x_vals = []
y_vals = []
with open(file) as f:
for row in f:
x, y = map(int, row.split(','))
x_vals.append(x)
y_vals.append(y)
return x_vals, y_vals
def read_data_col(self, file, event=None):
x_vals = []
y_vals = []
with open(file) as f:
pass
return x_vals, y_vals
def plot(self, event=None):
file = self.get_file()
print(file)
if os.path.exists(file):
if self.dataOrientVar.get() == "row":
x_vals, y_vals = self.read_data_row(file)
else:
x_vals, y_vals = self.read_data_col(file)
color = self.colorVar.get()
lnstyle = self.lineVar.get()
if self.interpolationVar.get():
spl = interp.CubicSpline(x_vals, y_vals)
xnew = np.linspace(x_vals[0], x_vals[-1], x_vals[-1]*20)
plt.plot(xnew, spl(xnew), color=color)
lnstyle="None"
plt.plot(x_vals, y_vals,
marker=self.markerVar.get(), linestyle=lnstyle, color=color)
plt.title(self.labelVar.get())
plt.xlabel(self.axisXVar.get())
plt.ylabel(self.axisYVar.get())
plt.show()
else:
print("SOUBOR NEBYL NALEZEN")
def quit(self, event=None):
super().quit()
app = Application()
app.mainloop()