-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindow.py
311 lines (264 loc) · 10.6 KB
/
window.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import tkinter as tk
import tkinter.ttk as ui
from tkinter import filedialog
import matplotlib.pyplot as plot
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import algorithms as alg
import numpy as numpy
class Window:
app = None
data = None
label = None
filename = None
# User Interface
downloadBtn = None
uploadDataBtn = None
clearDataBtn = None
dropdown = None
renderBtn = None
settingBtn = None
backgroundColor = None
fontColor = None
fontSize = None
settingsChangeLabel = None
# Graph Attributes
graphFigure = None
axes = None # Here we can specify which type we want through axes.bar, axes.line, etc.
graphCanvas = None
size = None # (x, y) tuple
def __init__(self, width, height):
self.width = width
self.height = height
self.backgroundColor = 'White'
self.fontColor = 'Black'
self.fontSize = '12'
def renderWindow(self):
# Basic Window
self.app = tk.Tk()
self.app.geometry(self.width + 'x' + self.height)
self.app.title('Data Reader')
self.app.resizable(width=0, height=0)
self.app.protocol("WM_DELETE_WINDOW", self.exit)
# Menu Button Frame
menuFrame = tk.LabelFrame(self.app, text='Menu Options', padx=10, pady=10, width=170, height=300, labelanchor='n')
menuFrame.pack(expand="yes", padx=20, pady=20, side='left')
menuFrame.propagate(False)
# Download PDF Button
self.downloadBtn = ui.Button(menuFrame, text='Download Data', command = self.downloadFrame)
self.downloadBtn.place(x=0, y=0)
self.downloadBtn.config(width = 20)
self.downloadBtn['state'] = 'disabled'
# Settings Button
self.settingBtn = ui.Button(menuFrame, text='Settings', command=self.openSettings)
self.settingBtn.place(x=0, y=40)
self.settingBtn.config(width = 20)
# Upload Data Button
self.uploadDataBtn = ui.Button(menuFrame, text='Upload Data', command=self.parseData)
self.uploadDataBtn.place(x=0, y=80)
self.uploadDataBtn.config(width=20)
# Dropdown Menu
self.dropdown = ui.Combobox(
menuFrame,
state = 'readonly',
values = [
'Bar',
'Pie',
'Line',
'Scatterplot'
]
)
self.dropdown.place(x=0, y=120)
self.dropdown.bind('<<ComboboxSelected>>', lambda event, arg=self.dropdown: self.setGraph(arg))
self.dropdown.current(0)
self.graphType = self.dropdown.get()
# Start Render Button
self.renderBtn = ui.Button(menuFrame, text='Start Render', command=self.startRender)
self.renderBtn.place(x=0, y=160)
self.renderBtn.config(width=20)
# Clear Data Button
self.clearDataBtn = ui.Button(menuFrame, text='Clear Graph Frame', command=self.clearGraphFrame)
self.clearDataBtn.place(x=0, y=200)
self.clearDataBtn.config(width=20)
self.clearDataBtn['state'] = 'disabled'
# Graph Frame
graphFrame = tk.LabelFrame(self.app, text='Data Enhancement', padx=10, pady=10, width=1100, height=500, labelanchor='n')
graphFrame.pack(expand="yes", padx=20, pady=20)
graphFrame.propagate(False)
# Add Empty Graph in graphFrame
self.graphFigure = plot.figure()
# Graph Canvas
self.graphCanvas = FigureCanvasTkAgg(self.graphFigure, graphFrame)
self.graphCanvas._tkcanvas.pack(fill=tk.BOTH, expand=1)
# Log Frame
logFrame = tk.LabelFrame(self.app, text='Logs', padx=10, pady=10, width=1500, height=200, labelanchor='n')
logFrame.pack(expand="yes", padx=20, pady=20)
logFrame.propagate(False)
# Log Label
self.label = tk.Label(logFrame, text='Welcome!')
self.label.pack(expand="yes", padx=20, pady=20)
self.label.config(font=("Arial", self.fontSize))
# Mainloop call at the end of the rendering
tk.mainloop()
def openSettings(self):
# Settings window
settings = tk.Tk()
settings.geometry('400x400')
settings.title('Settings')
settings.resizable(width=0, height=0)
# Settings includes background color change, font color change, and font size
settingsFrame = tk.LabelFrame(settings, text='Settings', padx=10, pady=10, width=250, height=300, labelanchor='n')
settingsFrame.pack(expand="yes", padx=20, pady=20, side='left')
settingsFrame.propagate(False)
# Background Color
bgColorLabel = tk.Label(settingsFrame, text='Background Color')
colors = [
'White',
'Black',
'Blue',
'Purple'
]
bgColorLabel.pack()
bgColor = ui.Combobox(
settingsFrame,
state = 'readonly',
values = colors
)
bgColor.place(x=20, y=20)
bgColor.pack(pady=5)
bgColor.bind('<<ComboboxSelected>>', lambda event, arg=bgColor: self.setBackgroundColor(arg))
bgColor.current(colors.index(self.backgroundColor))
# Font Color
fontColorLabel = tk.Label(settingsFrame, text='Font Color')
fontColors = [
'Black',
'White',
'Red',
'Purple'
]
fontColorLabel.pack()
fontColorDropdown = ui.Combobox(
settingsFrame,
state = 'readonly',
values = fontColors
)
fontColorDropdown.pack(pady=5)
fontColorDropdown.bind('<<ComboboxSelected>>', lambda event, arg=fontColorDropdown: self.setFontColor(arg))
fontColorDropdown.current(fontColors.index(self.fontColor))
# Font size
fontSizeLabel = tk.Label(settingsFrame, text='Log Font Size')
sizes = [
'12',
'14',
'16',
'18'
]
fontSizeLabel.pack()
fontSizeDropdown = ui.Combobox(
settingsFrame,
state = 'readonly',
values = sizes
)
fontSizeDropdown.pack(pady=5)
fontSizeDropdown.bind('<<ComboboxSelected>>', lambda event, arg=fontSizeDropdown: self.setFontSize(arg))
fontSizeDropdown.current(sizes.index(self.fontSize))
# Change settings label
self.settingsChangeLabel = tk.Label(settingsFrame)
self.settingsChangeLabel.pack(pady=5)
# Close Button
closeBtn = tk.Button(settingsFrame, text='Close', command=lambda: self.closeSettings(settings))
closeBtn.pack()
def parseData(self):
filetypes = (
('text files', '*.txt'),
('Excel files', '*.xlsx'),
('CSV Files', '*.csv')
)
self.filename = filedialog.askopenfilename(filetypes=filetypes)
if self.filename == '':
self.updateLogMessage('ERROR: No File Uploaded')
self.filename = None
return
self.updateLogMessage('SUCCESS: Uploaded File: ' + self.filename)
# Parse the data with an algorithm (depending on the file type)
if '.txt' in self.filename:
self.data = alg.parseTxt(self.filename)
elif '.csv' in self.filename:
self.data = alg.parseCSV(self.filename)
elif '.xlsx' in self.filename:
self.data = alg.parseExcel(self.filename)
def startRender(self):
if self.filename == None:
self.updateLogMessage('ERROR: There is No File Attached')
return
self.disableAll()
self.updateLogMessage('Rendering a ' + self.graphType + ' graph from file: ' + self.filename)
self.renderGraph()
def updateLogMessage(self, message):
if "ERROR" in message:
self.label.config(text=message, foreground='red')
return
elif "SUCCESS" in message:
self.label.config(text=message, foreground='green')
return
self.label.config(text=message, foreground='black')
def setGraph(self, combobox):
self.graphType = combobox.get()
def downloadFrame(self):
self.graphFigure.savefig('graph.png')
self.updateLogMessage('Downloaded Graph as PNG')
def clearGraphFrame(self):
self.graphFigure.clear()
self.graphCanvas.draw()
self.downloadBtn['state'] = 'disabled'
self.clearDataBtn['state'] = 'disabled'
self.updateLogMessage('Graph Frame Cleared')
def disableAll(self):
# Disable all the buttons
self.clearDataBtn['state'] = 'disabled'
self.downloadBtn['state'] = 'disabled'
self.renderBtn['state'] = 'disabled'
self.dropdown['state'] = 'disabled'
self.settingBtn['state'] = 'disabled'
self.uploadDataBtn['state'] = 'disabled'
def enableAll(self):
# Enable all the buttons
self.clearDataBtn['state'] = 'enabled'
self.downloadBtn['state'] = 'enabled'
self.renderBtn['state'] = 'enabled'
self.dropdown['state'] = 'enabled'
self.settingBtn['state'] = 'enabled'
self.uploadDataBtn['state'] = 'enabled'
def setBackgroundColor(self, combobox):
self.backgroundColor = combobox.get()
# Update the background color here - Closes the implement settings function issue
self.settingsChangeLabel.config(text='Background Color Updated Successfully')
def setFontColor(self, combobox):
self.fontColor = combobox.get()
# Update the font color here - Closes the implement settings function issue
self.settingsChangeLabel.config(text='Font Color Updated Successfully')
def setFontSize(self, combobox):
self.fontSize = combobox.get()
self.label.config(font=("Arial", self.fontSize))
self.settingsChangeLabel.config(text='Font Size Updated Successfully')
def closeSettings(self, settings):
settings.destroy()
# Add parameters for data, size, etc.
def renderGraph(self):
self.axes = self.graphFigure.add_subplot()
# The first element of 'x' and 'y' are the labels
self.axes.set_xlabel(self.data['x'][0])
self.axes.set_ylabel(self.data['y'][0])
# Mock test
data = (20, 35, 30, 35, 27)
ind = numpy.arange(5)
if self.graphType == 'Bar':
self.axes.bar(ind, data)
elif self.graphType == 'Line':
self.axes.plot(data)
elif self.graphType == 'Scatterplot':
self.axes.plot(self.data['x'][1:], self.data['y'][1:], "o")
self.graphCanvas.draw()
self.enableAll()
def exit(self):
self.app.quit()
self.app.destroy()