forked from edenvvv/Search-questions-DB
/
base.py
221 lines (175 loc) · 8.01 KB
/
base.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
import tkinter as tk
import DB as sql
from create_docx import create_word_file
import os
def main():
HEIGHT = 600
WIDTH = 1000
sql.create_table()
def create_folder(name, level, year):
"""Creates a folder in the specified location"""
new_path = f'C:/{name}/{level}/{year}'
if not os.path.exists(new_path):
os.makedirs(new_path)
def open_folder(name, level, year):
"""open a folder in the specified location"""
path = f'C:/{name}/{level}/{year}'
path = os.path.realpath(path)
os.startfile(path)
def create_word(course_name, question_name, difficult_level, year):
"""Creates a WORD document with the questions asked"""
try:
if course_name == '' or question_name == '':
# Checks if the received name course and question name is not empty
raise NameError
all_data = sql.get_test_info(course_name, question_name, difficult_level, year)
empty_list = []
if all_data == empty_list:
# Checks if the received name course and question name are the same as the database
raise ValueError
create_folder(question_name, difficult_level, year)
count = 1
word_list = []
for row in all_data:
photo_data = row[0]
# Saves the image information
photo_path = f'C:/{question_name}/{difficult_level}/{year}/{count}.jpg'
# Creates the location where the image will be saved
sql.write_to_file(photo_data, photo_path)
word_list.append(photo_path)
label.insert(tk.END, f"{count}) {row[1:]} ")
# print in label
count += 1
create_word_file(question_name, word_list)
except NameError:
mess = "Must enter course name and question name"
gui_print(mess)
except ValueError:
mess = "The names do not exist in the database"
gui_print(mess)
except Exception:
mess = "Something went wrong"
gui_print(mess)
def search_question(course_name, question_name, difficult_level, year):
"""Creates a folder with the questions asked"""
try:
if course_name == '' or question_name == '':
# Checks if the received name course and question name is not empty
raise NameError
all_data = sql.get_test_info(course_name, question_name, difficult_level, year)
empty_list = []
if all_data == empty_list:
# Checks if the received name course and question name are the same as the database
raise ValueError
create_folder(question_name, difficult_level, year)
count = 1
for row in all_data:
photo_data = row[0]
# Saves the image information
photo_path = f'C:/{question_name}/{difficult_level}/{year}/{count}.jpg'
# Creates the location where the image will be saved
sql.write_to_file(photo_data, photo_path)
label.insert(tk.END, f"{count}) {row[1:]} ")
# print in label
count += 1
open_folder(question_name, difficult_level, year)
except NameError:
mess = "Must enter course name and question name"
gui_print(mess)
except ValueError:
mess = "The names do not exist in the database"
gui_print(mess)
except Exception:
mess = "Something went wrong"
gui_print(mess)
def gui_print(message):
label.insert(tk.END, f"{message}, Try again")
# print in label
root = tk.Toplevel()
# Creates the program window
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
# Creates the window in "canvas" size
canvas.pack()
# Shows the window in "canvas" size
background_image = tk.PhotoImage(file='landscape.png')
# Takes a photo as input
background_label = tk.Label(root, image=background_image)
# Uses image as background
background_label.place(relwidth=1, relheight=1)
# Shows it in The specified coordinates
frame1 = tk.Frame(root, bg='#80c1ff', bd=5)
# Creates the frame Light blue (#80c1ff)
frame1.place(relx=0.2, rely=0.05, relwidth=0.3, relheight=0.15, anchor='n')
# Shows the frame in The specified coordinates
entry1 = tk.Entry(frame1, font=40)
# Creates a input box
entry1.place(rely=0.2, relwidth=1, relheight=0.8)
# Shows the input box
label1 = tk.Label(frame1, text='course name:', bg='#80c1ff', font=('courier', 10), anchor='nw', bd=4)
# Creates a label
label1.place(relx=0.12, rely=0, relwidth=0.35, relheight=0.3)
# Shows the label
frame2 = tk.Frame(root, bg='#80c1ff', bd=5)
# Creates the frame Light blue (#80c1ff)
frame2.place(relx=0.45, rely=0.05, relwidth=0.2, relheight=0.15, anchor='n')
# Shows the frame in The specified coordinates
entry2 = tk.Entry(frame2, font=40)
# Creates a input box
entry2.place(rely=0.2, relwidth=1, relheight=0.8)
# Shows the input box
label2 = tk.Label(frame2, text='question name:', bg='#80c1ff', font=('courier', 10), anchor='nw', bd=4)
# Creates a label
label2.place(relx=0.12, rely=0, relwidth=0.63, relheight=0.3)
# Shows the label
frame3 = tk.Frame(root, bg='#80c1ff', bd=5)
# Creates the frame Light blue (#80c1ff)
frame3.place(relx=0.65, rely=0.05, relwidth=0.2, relheight=0.15, anchor='n')
# Shows the frame in The specified coordinates
entry3 = tk.Entry(frame3, font=40)
# Creates a input box
entry3.place(rely=0.2, relwidth=1, relheight=0.8)
# Shows the input box
label3 = tk.Label(frame3, text='difficulty level:', bg='#80c1ff', font=('courier', 10), anchor='nw', bd=4)
# Creates a label
label3.place(relx=0.12, rely=0, relwidth=0.75, relheight=0.3)
# Shows the label
frame4 = tk.Frame(root, bg='#80c1ff', bd=5)
# Creates the frame Light blue (#80c1ff)
frame4.place(relx=0.85, rely=0.05, relwidth=0.2, relheight=0.15, anchor='n')
# Shows the frame in The specified coordinates
entry4 = tk.Entry(frame4, font=40)
# Creates a input box
entry4.place(rely=0.2, relwidth=0.5, relheight=0.8)
# Shows the input box
label4 = tk.Label(frame4, text='year:', bg='#80c1ff', font=('courier', 10), anchor='nw', bd=4)
# Creates a label
label4.place(relx=0.12, rely=0, relwidth=0.25, relheight=0.3)
# Shows the label
button = tk.Button(frame4, text="Search", font=40,
command=lambda: search_question(entry1.get(), entry2.get(), entry3.get(), entry4.get()))
# entry1.get() = course_name, entry2.get() = question name, entry3.get() = difficult_level, entry4.get() = year
# Creates a button
button.place(relx=0.55, rely=0.2, relwidth=0.45, relheight=0.8)
# Shows the button
lower_frame = tk.Frame(root, bg='#80c1ff', bd=10)
# Creates the lower frame Light blue (#80c1ff)
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
# Shows the lower frame in The specified coordinates
scrollbar = tk.Scrollbar(lower_frame)
# Creates a scrollbar
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Shows the scrollbar
label = tk.Listbox(lower_frame, font=('courier', 12), yscrollcommand=scrollbar.set)
# Creates a label
label.place(relwidth=0.9761, relheight=1)
# Shows the label
scrollbar.config(command=label.yview)
word_button = tk.Button(root, text="create word file", font=40,
command=lambda: create_word(entry1.get(), entry2.get(), entry3.get(), entry4.get()))
# entry1.get() = course_name, entry2.get() = question name, entry3.get() = difficult_level, entry4.get() = year
# Creates a button
word_button.place(relx=0.37, rely=0.87, relwidth=0.25, relheight=0.1)
# Shows the button
root.mainloop()
# Run the program window
sql.close_db()