-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
330 lines (262 loc) · 12.5 KB
/
app.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# app.py - program entry point
import tkinter as tk
from tkinter import filedialog, Menu, messagebox, ttk, font as tkfont
from time import sleep
from PIL import Image, ImageTk
import file
import os
import csv
class DocToCSV(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Tahoma', size=18, weight="bold")
self.footer_font = tkfont.Font(family='Tahoma', size=12)
window = tk.Frame(self)
window.pack(side="top", fill="both", expand=True)
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
menuBar = tk.Menu(self)
fileMenu = tk.Menu(menuBar, tearoff = False)
fileMenu.add_command(label = 'New File')
fileMenu.add_command(label = 'Open File...')
fileMenu.add('separator')
fileMenu.add_command(label = 'Quit', command = self.destroy)
helpMenu = tk.Menu(menuBar, tearoff = False)
helpMenu.add_command(label = 'Help')
helpMenu.add_command(label = 'About')
menuBar.add_cascade(label = 'File', menu = fileMenu)
menuBar.add_cascade(label = 'Help', menu = helpMenu)
self.config(menu = menuBar)
self.frames = {}
for F in (MainPage, FindFile, BreweryName, ConvertFile, PreviewData):
page_name = F.__name__
frame = F(parent=window, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("MainPage")
def show_frame(self, page_name):
global doc
global entry
frame = self.frames[page_name]
if "BreweryName" in page_name:
file_name = doc.get()
if not file.valid_file(file_name):
tk.messagebox.showwarning(title="Please select a file", message="You must select a file before proceeding.")
else:
frame.tkraise()
if "ConvertFile" in page_name:
brew_name = entry.get()
if not file.valid_name(brew_name):
tk.messagebox.showwarning(title="Please enter a name", message="You must select a brewery name before proceeding.")
else:
frame.tkraise()
elif "MainPage" in page_name or "FindFile" in page_name or "PreviewData" in page_name:
frame.tkraise()
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
topFrame = tk.Frame(self)
midFrame = tk.Frame(self)
botFrame = tk.Frame(self)
topFrame.pack(side="top", fill="both", expand=True)
botFrame.pack(side="bottom", fill="both", expand=True)
midFrame.pack(side="bottom", fill="both", expand=True)
self.logoPath = 'gfx/logo.png'
self.logoImg = ImageTk.PhotoImage(Image.open(self.logoPath))
self.label_main = tk.Label(topFrame, text="Main Menu", font=controller.title_font, image=self.logoImg)
self.label_main.pack(side="top", fill="x", pady=1)
button_next = tk.Button(midFrame, text="Start Converting", command=lambda: controller.show_frame("FindFile"), padx=10, pady=5)
button_next.pack(side="top", padx=10, pady=10)
label_cr = tk.Label(botFrame, text="The Brewery Pioneers, Inc \u00a9", font=controller.footer_font)
label_cr.pack(side="bottom", padx=10, pady=10)
class FindFile(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
global doc
global path
topFrame = tk.Frame(self)
midFrame = tk.Frame(self)
botFrame = tk.Frame(self)
topFrame.pack(side="top", fill="both", expand=True)
botFrame.pack(side="bottom", fill="both", expand=True)
midFrame.pack(side="bottom", fill="both", expand=True)
doc = tk.StringVar()
doc.set("")
path = ""
label = tk.Label(topFrame, text="Get Document File", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button_find = tk.Button(topFrame, text="Browse", command=self.find_doc, padx=10, pady=5)
button_find.pack(side="left", padx=20, pady=10)
entry_find = tk.Entry(topFrame, textvariable=doc, width=60)
entry_find.pack(side="left", padx=10, pady=10)
button_main = tk.Button(midFrame, text="Main Menu", command=self.main_func, padx=10, pady=5)
button_next = tk.Button(midFrame, text="Next", command=lambda: controller.show_frame("BreweryName"), padx=10, pady=5)
button_main.pack(side="bottom", padx=5, pady=5)
button_next.pack(side="bottom", padx=5, pady=5)
label_cr = tk.Label(botFrame, text="The Brewery Pioneers, Inc \u00a9", font=controller.footer_font)
label_cr.pack(side="bottom", padx=10, pady=10)
def find_doc(self):
global doc
global path
this_doc = tk.filedialog.askopenfilename(initialdir="/", title="Select a file", filetypes=(("Microsoft Word (2007 - 365)", "*.docx"),("Legacy Microsoft Word (97-2000)", "*.doc")))
my_file = os.path.basename(this_doc)
doc.set(my_file)
path = os.path.abspath(this_doc)
def main_func(self):
global doc
self.controller.show_frame("MainPage")
doc.set("")
class BreweryName(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
global entry_txt
global entry
topFrame = tk.Frame(self)
midFrame = tk.Frame(self)
botFrame = tk.Frame(self)
topFrame.pack(side="top", anchor="center")
botFrame.pack(side="bottom", fill="both", expand=True)
midFrame.pack(side="bottom", fill="both", expand=True)
label = tk.Label(topFrame, text="Brewery Name", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
label_entry = tk.Label(topFrame, text="Enter the Brewery Name: ")
entry = tk.Entry(topFrame)
label_entry.pack(side="left",fill="none", expand=True,padx = 10)
entry.pack(side="left", fill="none", expand=True, padx = 10)
button_main = tk.Button(midFrame, text="Main Menu", command=self.main_func, padx=10, pady=5)
button_prev = tk.Button(midFrame, text="Previous", command=lambda: controller.show_frame("FindFile"), padx=10, pady=5)
button_next = tk.Button(midFrame, text="Next", command=self.next_func, padx=10, pady=5)
button_main.pack(side="bottom", padx=5, pady=5)
button_prev.pack(side="bottom", padx=5, pady=5)
button_next.pack(side="bottom", padx=5, pady=5)
label_cr = tk.Label(botFrame, text="The Brewery Pioneers, Inc \u00a9", font=controller.footer_font)
label_cr.pack(side="bottom", padx=10, pady=10)
def next_func(self):
global entry_txt
global entry
self.controller.show_frame("ConvertFile")
entry_txt = entry.get()
def main_func(self):
global entry
global doc
self.controller.show_frame("MainPage")
entry.delete(0, tk.END)
doc.set("")
class ConvertFile(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
global progress
global progress_txt
progress = tk.DoubleVar()
progress_txt = tk.StringVar()
topFrame = tk.Frame(self)
midFrame = tk.Frame(self)
botFrame = tk.Frame(self)
topFrame.pack(side="top", fill="both", expand=True)
botFrame.pack(side="bottom", fill="both", expand=True)
midFrame.pack(side="bottom", fill="both", expand=True)
label = tk.Label(topFrame, text="Convert File", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button_find = tk.Button(midFrame, text="Convert File", command=self.start_convert, padx=10, pady=5)
button_find.pack(side="top", padx=10, pady=10)
progress_pb = ttk.Progressbar(midFrame, orient="horizontal", length=300, mode='determinate', maximum=100, variable=progress)
progress_text = tk.Label(midFrame, textvariable=progress_txt)
button_main = tk.Button(midFrame, text="Main Menu", command=lambda: controller.show_frame("MainPage"), padx=10, pady=5)
button_prev = tk.Button(midFrame, text="Previous", command=lambda: controller.show_frame("BreweryName"), padx=10, pady=5)
button_next = tk.Button(midFrame, text="Next", command=lambda: controller.show_frame("PreviewData"), padx=10, pady=5)
button_main.pack(side="bottom", padx=5, pady=5)
button_prev.pack(side="bottom", padx=5, pady=5)
button_next.pack(side="bottom", padx=5, pady=5)
progress_text.pack(side="bottom", padx=10, pady=10)
progress_pb.pack(side="bottom", padx=10, pady=10)
label_cr = tk.Label(botFrame, text="The Brewery Pioneers, Inc \u00a9", font=controller.footer_font)
label_cr.pack(side="bottom", padx=10, pady=10)
def start_convert(self):
global path
global progress
global progress_txt
global entry_txt
my_doc = path
my_par = file.do_Doc(my_doc)
i = 0
while i < my_par+1:
my_prog = round((i/my_par)*100)
str_prog = str(my_prog)+ " %"
progress.set(my_prog)
if i == my_par:
progress_txt.set("Conversion is complete")
else:
progress_txt.set(str_prog)
i = i+1
self.update()
#self.after(1000)
my_brew = entry_txt
file.do_CSV(my_par, my_doc, my_brew)
class PreviewData(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
topFrame = tk.Frame(self)
midFrame = tk.Frame(self)
botFrame = tk.Frame(self)
topFrame.pack(side="top", fill="both", expand=True)
botFrame.pack(side="bottom", fill="both", expand=True)
midFrame.pack(side="bottom", fill="both", expand=True)
label = tk.Label(topFrame, text="Preview Data", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button_show = tk.Button(midFrame, text="Show Data", command=self.show_data, padx=10, pady=5)
button_show.pack(side="top", padx=10, pady=10)
button_main = tk.Button(midFrame, text="Main Menu", command=lambda: controller.show_frame("MainPage"), padx=10, pady=5)
button_prev = tk.Button(midFrame, text="Previous", command=lambda: controller.show_frame("ConvertFile"), padx=10, pady=5)
button_main.pack(side="bottom", padx=5, pady=5)
button_prev.pack(side="bottom", padx=5, pady=5)
label_cr = tk.Label(botFrame, text="The Brewery Pioneers, Inc \u00a9", font=controller.footer_font)
label_cr.pack(side="bottom", padx=10, pady=10)
def show_data(self):
my_csv = file.csv_preview()
pws = app.winfo_screenwidth()
phs = app.winfo_screenheight()
if pws < 1000:
pw = 500
elif pws >= 1000:
pw = 1000
ph = 500
px = (pws/2) + (pw*2)
py = (phs/2) - (ph*2)
window = tk.Toplevel(self)
window.title("CSV Data to Import")
window.geometry('%dx%d+%d+%d' % (pw, ph, px, py))
with open(my_csv, newline = "") as csvfile:
reader = csv.reader(csvfile)
rowNum = 0
for col in reader:
colNum = 0
for row in col:
if rowNum == 0:
tbl = tk.Label(window, width=len(row)+2, height=2, text=row, relief=tk.RIDGE, font="Tahoma 14 bold")
else:
tbl = tk.Label(window, width=len(row)+2, height=2, text=row, relief=tk.RIDGE)
tbl.grid(row=rowNum, column=colNum, sticky="nsew")
colNum += 1
rowNum += 1
if __name__ == "__main__":
app = DocToCSV()
app.title("The Brewery Pioneers: CSV Import")
w = 400
h = 350
# get screen width and height
ws = app.winfo_screenwidth()
hs = app.winfo_screenheight()
# calculate x and y coordinates for the Tk root window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
# set the dimensions of the screen and where it is placed
app.geometry('%dx%d+%d+%d' % (w, h, x, y))
app.mainloop()