-
Notifications
You must be signed in to change notification settings - Fork 0
/
tixex.py
313 lines (246 loc) · 13.7 KB
/
tixex.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
import tkinter as tk
from tkinter import messagebox
class MenuBar(tk.Menu):
def __init__(self, master):
super().__init__(master)
# create the menu items
add_menu = tk.Menu(self, tearoff=0)
show_menu = tk.Menu(self, tearoff=0)
edit_menu = tk.Menu(self, tearoff=0)
search_menu = tk.Menu(self, tearoff=0)
delete_menu = tk.Menu(self, tearoff=0)
look_menu = tk.Menu(self, tearoff=0)
# add the options to each menu
add_menu.add_command(label="Add Trainee", command=self.show_add_form)
show_menu.add_command(label="Show Trainee", command=self.show_trainee_profiles)
delete_menu.add_command(label="Delete Trainee", command=self.show_delete_form)
edit_menu.add_command(label="Edit Trainee", command=self.show_edit_form)
search_menu.add_command(label="Search for Trainee by the Name", command=self.search_trainee_by_name)
search_menu.add_command(label="Search for Trainee by the Age", command=self.search_trainee_by_age)
look_menu.add_command(label="Look up for Trainee", command=self.look_up)
# add the menus to the menu bar
self.add_cascade(label="ADD", menu=add_menu)
self.add_cascade(label="SHOW", menu=show_menu)
self.add_cascade(label="DELETE", menu=delete_menu)
self.add_cascade(label="EDIT", menu=edit_menu)
self.add_cascade(label="SEARCH", menu=search_menu)
self.add_cascade(label="LOOK UP", menu=look_menu)
# add the exit option to the menu bar
self.add_command(label="EXIT", command=master.quit)
# initialize trainee data structure
self.trainees = []
def show_add_form(self):
# create a new window for the form
form_window = tk.Toplevel(self.master)
# add the form elements
tk.Label(form_window, text="Num:").grid(row=0, column=0)
num_entry = tk.Entry(form_window)
num_entry.grid(row=0, column=1)
tk.Label(form_window, text="Name:").grid(row=1, column=0)
name_entry = tk.Entry(form_window)
name_entry.grid(row=1, column=1)
tk.Label(form_window, text="Age:").grid(row=2, column=0)
age_entry = tk.Entry(form_window)
age_entry.grid(row=2, column=1)
submit_button = tk.Button(form_window, text="Submit",command=lambda: self.add_trainee(num_entry.get(), name_entry.get(),age_entry.get(), form_window))
submit_button.grid(row=3, column=1)
def add_trainee(self, num, name, age, form_window):
# add trainee to data structure
self.trainees.append({'Num': num, 'Name': name, 'Age': age})
# display message box
messagebox.showinfo("Success", "Trainee added successfully.")
# clear form entries
form_window.destroy()
def show_trainee_profiles(self):
# create a new window for the trainee profiles
profile_window = tk.Toplevel(self.master)
# add the trainee profiles
for i, trainee in enumerate(self.trainees):
tk.Label(profile_window, text=f"Trainee {i + 1}:").grid(row=i, column=0)
tk.Label(profile_window, text=f"Num: {trainee['Num']}, Name: {trainee['Name']}, Age: {trainee['Age']}").grid(row=i,column=1)
# add a message if there are no trainee profiles
if not self.trainees:
tk.Label(profile_window, text="No trainee profiles available.", font=("Arial Bold", 12)).grid(row=0,column=0)
def show_delete_form(self):
# create a new window for the form
form_window = tk.Toplevel(self.master)
# add the form elements
tk.Label(form_window, text="Num:").grid(row=0, column=0)
num_entry = tk.Entry(form_window)
num_entry.grid(row=0, column=1)
submit_button = tk.Button(form_window, text="Submit",command=lambda: self.delete_trainee(num_entry.get(), form_window))
submit_button.grid(row=1, column=1)
def delete_trainee(self, num, form_window):
# search for the trainee with the given num
for trainee in self.trainees:
if trainee['Num'] == num:
# remove the trainee from the data structure
self.trainees.remove(trainee)
# display message box
messagebox.showinfo("Success", "Trainee deleted successfully.")
# clear form entries
form_window.destroy()
return
# display error message if trainee with given num not found
messagebox.showerror("Error", "Trainee not found.")
def show_edit_form(self):
# create a new window for the form
edit_window = tk.Toplevel(self.master)
# add the form elements
tk.Label(edit_window, text="Enter trainee number:").grid(row=0, column=0)
num_entry = tk.Entry(edit_window)
num_entry.grid(row=0, column=1)
submit_button = tk.Button(edit_window, text="Submit",command=lambda: self.edit_trainee(num_entry.get(), edit_window))
submit_button.grid(row=1, column=1)
def edit_trainee(self, num, edit_window):
# search for the trainee with the given num
for trainee in self.trainees:
if trainee['Num'] == num:
# create a new window for editing the trainee profile
edit_form_window = tk.Toplevel(self.master)
# add the form elements with the trainee information pre-filled
tk.Label(edit_form_window, text="Num:").grid(row=0, column=0)
num_entry = tk.Entry(edit_form_window, state="disabled")
num_entry.grid(row=0, column=1)
num_entry.insert(0, trainee['Num'])
tk.Label(edit_form_window, text="Name:").grid(row=1, column=0)
name_entry = tk.Entry(edit_form_window)
name_entry.grid(row=1, column=1)
name_entry.insert(0, trainee['Name'])
tk.Label(edit_form_window, text="Age:").grid(row=2, column=0)
age_entry = tk.Entry(edit_form_window)
age_entry.grid(row=2, column=1)
age_entry.insert(0, trainee['Age'])
# add a "Save" button to save the changes
save_button = tk.Button(edit_form_window, text="Save",command=lambda: self.save_trainee(trainee, name_entry.get(), age_entry.get(),edit_form_window))
save_button.grid(row=3, column=1)
return
# display error message if trainee with given num not found
messagebox.showerror("Error", "Trainee not found.")
def save_trainee(self, trainee, name, age, edit_form_window):
# modify the trainee profile with the new information
trainee['Name'] = name
trainee['Age'] = age
# display success message
messagebox.showinfo("Success", "Trainee updated successfully.")
# close the edit form window
edit_form_window.destroy()
def search_trainee_by_name(self):
# create a new window for the search form
search_window = tk.Toplevel(self.master)
# add the search form elements
tk.Label(search_window, text="Name:").grid(row=0, column=0)
name_entry = tk.Entry(search_window)
name_entry.grid(row=0, column=1)
submit_button = tk.Button(search_window, text="Submit", command=lambda: self.display_trainees_by_name(name_entry.get(), search_window))
submit_button.grid(row=1, column=1)
def display_trainees_by_name(self, name, search_window):
# create a new window for the search results
results_window = tk.Toplevel(self.master)
# add the search results
for i, trainee in enumerate(self.trainees):
if trainee['Name'].lower() == name.lower():
tk.Label(results_window, text=f"Trainee {i + 1}:").grid(row=i, column=0)
tk.Label(results_window,text=f"Num: {trainee['Num']}, Name: {trainee['Name']}, Age: {trainee['Age']}").grid(row=i,column=1)
# add a message if there are no search results
if not any(trainee['Name'].lower() == name.lower() for trainee in self.trainees):
tk.Label(results_window, text="No trainees with that name found.").grid(row=0, column=0)
# clear form entries and destroy search window
search_window.destroy()
def search_trainee_by_age(self):
# create a new window for the search form
search_window = tk.Toplevel(self.master)
# add the search form elements
tk.Label(search_window, text="Age:").grid(row=0, column=0)
age_entry = tk.Entry(search_window)
age_entry.grid(row=0, column=1)
submit_button = tk.Button(search_window, text="Submit",command=lambda: self.display_trainees_by_age(age_entry.get(), search_window))
submit_button.grid(row=1, column=1)
def display_trainees_by_age(self, age, search_window):
# create a new window for the search results
results_window = tk.Toplevel(self.master)
# add the search results
for i, trainee in enumerate(self.trainees):
if trainee['Age'] == age:
tk.Label(results_window, text=f"Trainee {i + 1}:").grid(row=i, column=0)
tk.Label(results_window,
text=f"Num: {trainee['Num']}, Name: {trainee['Name']}, Age: {trainee['Age']}").grid(row=i,column=1)
# add a message if there are no search results
if not any(trainee['Age'] == age for trainee in self.trainees):
tk.Label(results_window, text="No trainees with that age found.").grid(row=0, column=0)
# clear form entries and destroy search window
search_window.destroy()
def look_up(self):
# create a new window for the search results
look_window = tk.Toplevel(self.master)
ra = tk.StringVar(value='E')
# add the radio form elements
age_entry = tk.Radiobutton(look_window, text=": Age", value='A', variable=ra)
age_entry.grid(row=0, column=0)
name_entry = tk.Radiobutton(look_window, text=": Name", value='N', variable=ra)
name_entry.grid(row=0, column=2)
def submit():
selected_option = ra.get()
if selected_option == 'A':
self.display_lookupN(look_window)
elif selected_option == 'N':
self.display_lookupN(look_window)
submit_button = tk.Button(look_window, text="Submit", command=submit)
submit_button.grid(row=1, column=1)
def display_lookupA(self,age):
# create a new window for the search form
search_window = tk.Toplevel(self.master)
# add the search form elements
tk.Label(search_window, text="Age:").grid(row=0, column=0)
age_entry = tk.Entry(search_window)
age_entry.grid(row=0, column=1)
submit_button = tk.Button(search_window, text="Submit",
command=lambda: self.look_trainees_by_age(age_entry.get(), search_window))
submit_button.grid(row=1, column=1)
def look_trainees_by_age(self, age, search_window):
# create a new window for the search results
results_window = tk.Toplevel(self.master)
# add the search results
for i, trainee in enumerate(self.trainees):
if trainee['Age'] == age:
tk.Label(results_window, text=f"Trainee {i + 1}:").grid(row=i, column=0)
tk.Label(results_window,
text=f"Num: {trainee['Num']}, Name: {trainee['Name']}, Age: {trainee['Age']}").grid(row=i,
column=1)
# add a message if there are no search results
if not any(trainee['Age'] == age for trainee in self.trainees):
tk.Label(results_window, text="No trainees with that age found.").grid(row=0, column=0)
# clear form entries and destroy search window
search_window.destroy()
def display_lookupN(self,name):
# create a new window for the search form
search_window = tk.Toplevel(self.master)
# add the search form elements
tk.Label(search_window, text="Name:").grid(row=0, column=0)
name_entry = tk.Entry(search_window)
name_entry.grid(row=0, column=1)
submit_button = tk.Button(search_window, text="Submit",
command=lambda: self.look_trainees_by_name(name_entry.get(), search_window))
submit_button.grid(row=1, column=1)
def look_trainees_by_name(self, name, search_window):
# create a new window for the search results
results_window = tk.Toplevel(self.master)
# add the search results
for i, trainee in enumerate(self.trainees):
if trainee['Name'].lower() == name.lower():
tk.Label(results_window, text=f"Trainee {i + 1}:").grid(row=i, column=0)
tk.Label(results_window,text=f"Num: {trainee['Num']}, Name: {trainee['Name']}, Age: {trainee['Age']}").grid(row=i,column=1)
# add a message if there are no search results
if not any(trainee['Name'].lower() == name.lower() for trainee in self.trainees):
tk.Label(results_window, text="No trainees with that name found.").grid(row=0, column=0)
# clear form entries and destroy search window
search_window.destroy()
def main():
root = tk.Tk()
root.title("Trainee Gestion")
root.geometry("350x200")
menubar = MenuBar(root)
root.config(menu=menubar)
root.mainloop()
if __name__ == "__main__":
main()