-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
841 lines (689 loc) · 25.7 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
"""
Author: Jonathan Ransbottom
Date: 01/21/2025
Version: v1.0.0
Python Version: 3.13.1
Description:
This is the Parts Inventory desktop application. The application is used to
manage each parts inventory to better provide accountablility for company owned parts.
Each employee is responsible for his/her parts inventory and providing a random accessment of their
parts monthly. This application will provide a place to enter part number, quantity, and description.
Employees can search their inventory by using the search function and export the entire database to excel.
They can also utilize an existing excel spreadsheet to import their parts inventory following the proper
table format (Part, Quantity, Description).
"""
####################################################################################################
# Import module functions.
import tkinter as tk
from tkinter import *
from tkinter import messagebox # This will allow us to display message boxes.
from tkinter import filedialog, ttk
import customtkinter
import pandas as pd # This will allow us to use the pandas module for data manipulation such as exporting to excel. Also installed openpyxl.
from customtkinter import *
from PIL import Image, ImageTk
import database
####################################################################################################
########### Creating color pallette in case I want to use them. ####################################
# Default colors.
color_def1 = "#ffffff"
color_def2 = "#000000"
color_def3 = "lightgray"
color_def4 = "silver"
# Most used colors.
color_1 = "#6a6262" # background color for main window and behind button/text
color_2 = "#4b3b47" # background color header
color_3 = "#9c9990" # background color for treeview odd rows
color_4 = "#e0d8de" # Button hover color light orange
color_5 = "#cfd2b2" # used a lot on button bg and fg colors
####################################################################################################
########### Creating the Window Frame ##############################################################
root = tk.Tk()
root.geometry("900x600+300+250") # This will set the size of the window.
root.configure(bg=color_1)
root.resizable(0, 0) # This will make the window non-resizable.
root.title("Parts Inventory Application") # This will set the title of the window.
root.iconbitmap("assets/images/pyspider73.ico") # Windows Icon
root.protocol("WM_DELETE_WINDOW", lambda: None) # This will disable the close button.
####################################################################################################
########### Creating fonts in case I want to use them. #############################################
# ****************************************************************************************************
font_1 = customtkinter.CTkFont(family="Arial", size=36, weight="bold")
font_2 = customtkinter.CTkFont(family="Arial", size=12, weight="bold")
font_3 = customtkinter.CTkFont(family="Arial", size=18, weight="bold")
font_4 = customtkinter.CTkFont(family="Arial", size=14, weight="bold")
font_5 = customtkinter.CTkFont(family="Arial", size=20, weight="bold")
font_6 = customtkinter.CTkFont(family="Arial", size=16, weight="normal", slant="italic")
font_7 = customtkinter.CTkFont(family="Arial", size=16, weight="bold")
####################################################################################################
########### Functions for Buttons ##################################################################
####################################################################################################
# Define the Add New Button on side frame.
def insert():
part_number = part_numberEntry.get()
quantity = quantityEntry.get()
description = descriptionEntry.get()
if not (part_number and quantity and description):
messagebox.showerror("Error", "All fields are required")
elif database.part_numbers_exists(part_number):
messagebox.showerror("Error", "Part number already exists.")
else:
try:
part_number_value = str(part_number)
database.insert_part_numbers(part_number_value, quantity, description)
tree.delete(*tree.get_children())
add_to_treeview()
messagebox.showinfo("Success", "Data has been inserted.")
except TypeError:
messagebox.showerror("Error", "Part Number should be an integer.")
####################################################################################################
# Define the Update Button on side frame.
def update_inventory():
selected_item = tree.focus()
if selected_item is None:
messagebox.showerror("Error", "Choose a part to update.")
else:
part_number = part_numberEntry.get()
quantity = quantityEntry.get()
description = descriptionEntry.get()
if not (part_number and quantity and description):
messagebox.showerror("Error", "All fields are required")
else:
try:
part_number_value = str(part_number)
database.update_inventory(part_number_value, quantity, description)
tree.delete(*tree.get_children())
add_to_treeview()
messagebox.showinfo("Success", "Data has been updated.")
except ValueError:
messagebox.showerror("Error", "Part Number should be a string.")
####################################################################################################
# Define the Clear Button.
def clear(*clicked):
if clicked:
tree.selection_remove(tree.focus(""))
part_numberEntry.delete(0, END)
quantityEntry.delete(0, END)
descriptionEntry.delete(0, END)
####################################################################################################
# Define the Search Button.
def search_part_numbers():
if searchEntry.get() == "":
messagebox.showerror("Error", "Enter value to search")
elif searchBox.get() == "Search by":
messagebox.showerror("Error", "Please select an option")
else:
search_by = searchBox.get()
search_value = searchEntry.get()
# Map display values to actual column names
column_map = {
"Part Number": "part_number",
"Description": "description",
"Quantity": "quantity",
}
search_by_column = column_map.get(search_by)
print(f"Searching by: {search_by_column}, Value: {search_value}") # Debug print
searched_inventory = database.search(search_by_column, search_value)
print(f"Search results: {searched_inventory}") # Debug print
# Clear the treeview
tree.delete(*tree.get_children())
# Insert the search results into the treeview
for part in searched_inventory:
tree.insert("", END, values=part)
####################################################################################################
# Define the Show All Button.
def show_all():
add_to_treeview()
searchEntry.delete(0, END)
searchEntry.insert(0, "")
searchBox.set("Search By")
####################################################################################################
# Define the Export to Excel Button.
def export():
part_numbers = database.fetch_inventory()
if not part_numbers:
messagebox.showerror("Error", "No data available to export.")
return
df = pd.DataFrame(part_numbers, columns=["Part Number", "Quantity", "Description"])
file_path = (
filedialog.asksaveasfilename( # Ask user to save file and where to save it.
defaultextension=".xlsx",
filetypes=[("Excel files", "*.xlsx"), ("All files", "*.*")],
initialfile="parts_inventory.xlsx",
initialdir="~/Desktop",
)
)
if not file_path:
return
try:
df.to_excel(file_path, index=False)
messagebox.showinfo("Success", f"Data exported successfully to {file_path}")
except Exception as e:
messagebox.showerror("Error", f"Failed to export data: {e}")
####################################################################################################
# Define the Import from Excel Button.
def import_data():
file_path = filedialog.askopenfilename(
filetypes=[("Excel files", "*.xlsx"), ("All files", "*.*")]
)
if not file_path:
return
try:
df = pd.read_excel(file_path)
for _, row in df.iterrows():
part_number = row["Part Number"]
quantity = row["Quantity"]
description = row["Description"]
if not database.part_numbers_exists(part_number):
database.insert_part_numbers(part_number, quantity, description)
add_to_treeview()
messagebox.showinfo("Success", "Data imported successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to import data: {e}")
####################################################################################################
# Define the DELETE Button.
def delete():
"""
Deletes the selected part from the database.
"""
selected_items = tree.selection()
print(f"Selected items: {selected_items}") # Debugging statement
if not selected_items:
messagebox.showerror("Error", "Choose a part to delete.")
else:
values = tree.item(selected_items[0]).get("values", [])
print(f"Values: {values}") # Debugging statement
if not values:
messagebox.showerror("Error", "Selected item has no values.")
else:
part_number = values[0]
try:
part_number_value = str(part_number)
database.delete_inventory(part_number_value)
tree.delete(selected_items[0]) # Remove the item from the tree view
tree.delete(*tree.get_children()) # Clear the treeview
add_to_treeview() # Refresh the treeview
messagebox.showinfo("Success", "Part deleted successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to delete part: {e}")
####################################################################################################
# Define the DELETE ALL Button.
def deleteAll():
print("DELETE ALL function called")
confirm = messagebox.askyesnocancel(
title="Delete All Inventory",
message="Are you sure you want to DELETE ALL INVENTORY in the DATABASE? \nThis action cannot be undone!!!",
)
if confirm:
database.delete_all_inventory()
add_to_treeview()
tree.delete(*tree.get_children())
messagebox.showinfo("Info", "All Parts Deleted")
elif confirm is None:
print("Action cancelled")
else:
print("No action taken")
####################################################################################################
########## Top Frame ###############################################################################
####################################################################################################
# This will create a frame for the top frame.
root.topFrame = Frame(root, bg=color_2)
root.topFrame.place(x=0, y=0, width=900, height=90)
textLable = Label(
root.topFrame,
text="Parts Inventory",
font=(font_1),
bg=color_2,
fg=color_def1,
)
textLable.place(x=20, y=10)
textLable = Label(
root.topFrame,
text="Parts Inventory Management Application",
font=(font_6),
bg=color_2,
fg=color_5,
)
textLable.place(x=20, y=60)
####################################################################################################
# This will create a button 'Logout' to Logout of the window.
logout24 = customtkinter.CTkImage(Image.open("assets/images/logout24.png"))
root.logoutLink = customtkinter.CTkButton(
root.topFrame,
text_color=color_def1,
bg_color=color_2,
fg_color=color_2,
hover_color=color_2, # Set the hover color for the button
width=20,
height=4,
image=logout24,
compound=tk.LEFT,
cursor="hand2",
text="Logout",
font=(font_5),
command=root.quit, # Used to close the program for "Logout".
)
root.logoutLink.place(x=785, y=25)
####################################################################################################
######### END OF MAIN WINDOW #######################################################################
####################################################################################################
####################################################################################################
######### midFrame1 ###############################################################################
####################################################################################################
root.midFrame1 = customtkinter.CTkFrame(
root,
bg_color=color_1,
fg_color=color_5,
corner_radius=10,
border_width=4,
border_color=color_2,
width=400,
height=130,
)
####################################################################################################
####### Text Fields Labels #########################################################################
####################################################################################################
textLable = Label(
root.midFrame1,
text="Part Number",
font=(font_2),
bg=color_5,
fg=color_def2,
)
textLable.place(x=10, y=5)
textLable = Label(
root.midFrame1,
text="Quantity",
font=(font_2),
bg=color_5,
fg=color_def2,
)
textLable.place(x=120, y=5)
textLable = Label(
root.midFrame1,
text="Description",
font=(font_2),
bg=color_5,
fg=color_def2,
)
textLable.place(x=200, y=5)
####################################################################################################
####### Text Entry Fields ##########################################################################
#################################################################################################
# This is the Part Entry.
part_numberEntry = entry = customtkinter.CTkEntry(
root,
width=100,
height=28,
bg_color=color_5,
fg_color=color_def1,
text_color=color_def2,
)
entry.place(x=30, y=125)
#################################################################################################
# This is the Quantity Entry.
quantityEntry = entry = customtkinter.CTkEntry(
root,
width=70,
height=28,
bg_color=color_5,
fg_color=color_def1,
text_color=color_def2,
)
entry.place(x=140, y=125)
#################################################################################################
# This is the Description Entry.
descriptionEntry = entry = customtkinter.CTkEntry(
root,
width=190,
height=28,
bg_color=color_5,
fg_color=color_def1,
text_color=color_def2,
)
entry.place(x=220, y=125)
root.midFrame1.place(
x=20,
y=100,
)
####################################################################################################
####### Buttons ####################################################################################
####################################################################################################
# This is the Add New Button.
root.addLink = customtkinter.CTkButton(
root.midFrame1,
corner_radius=50,
text_color=color_def2,
bg_color=color_5,
fg_color=color_4,
hover_color=color_def3, # Set the hover color for the button
width=100,
height=4,
border_color=color_2,
border_width=2,
cursor="hand2",
text="Add New",
font=(font_3),
command=lambda: (insert(), clear(True)),
)
root.addLink.place(x=30, y=60)
####################################################################################################
# This is the Update Button.
root.updateLink = customtkinter.CTkButton(
root.midFrame1,
text_color=color_def2,
bg_color=color_5,
fg_color=color_4,
hover_color=color_def3, # Set the hover color for the button
# corner_radius=30,
width=100,
height=4,
border_color=color_2,
border_width=2,
cursor="hand2",
text="Update",
font=(font_3),
command=lambda: (update_inventory(), clear(True)),
# This will update the fields and then clear the text fields after button pressed.
)
root.updateLink.place(x=140, y=60)
####################################################################################################
# This is the Clear Button.
root.clearLink = customtkinter.CTkButton(
root.midFrame1,
text_color=color_def2,
bg_color=color_5,
fg_color=color_4,
hover_color=color_def3, # Set the hover color for the button
width=210,
height=4,
border_color=color_2,
border_width=2,
cursor="hand2",
text="Clear Fields",
font=(font_3),
command=lambda: clear(True),
)
root.clearLink.place(x=30, y=95)
####################################################################################################
# This is the Delete Button.
root.deleteLink = customtkinter.CTkButton(
root.midFrame1,
text_color=color_def2,
bg_color=color_5,
fg_color=color_4,
hover_color=color_def3, # Set the hover color for the button
# corner_radius=30,
width=100,
height=4,
border_color=color_2,
border_width=2,
cursor="hand2",
text="Delete",
font=(font_3),
command=lambda: (delete(), clear(True)),
)
root.deleteLink.place(x=260, y=60)
####################################################################################################
# This is the Delete All Button.
root.deleteAllLink = customtkinter.CTkButton(
root.midFrame1,
text_color=color_def2,
bg_color=color_5,
fg_color=color_4,
hover_color=color_def3, # Set the hover color for the button
# corner_radius=30,
width=100,
height=4,
border_color=color_2,
border_width=2,
cursor="hand2",
text="Delete All",
font=(font_3),
command=lambda: (deleteAll(), clear(True)),
)
root.deleteAllLink.place(x=260, y=95)
####################################################################################################
######### midFrame2 ###############################################################################
####################################################################################################
root.midFrame2 = customtkinter.CTkFrame(
root,
bg_color=color_1,
fg_color=color_5,
corner_radius=10,
border_width=4,
border_color=color_2,
width=400,
height=130,
)
root.midFrame2.place(
x=475,
y=100,
)
####################################################################################################
# This is the Search Label.
textLable = Label(
root.midFrame2,
text="Search Inventory",
font=(font_7),
bg=color_5,
fg=color_def2,
)
textLable.place(x=80, y=15)
####################################################################################################
# This is the Search ComboBox.
def search_by_part_number(part_number):
return database.search("part_number", part_number)
def search_by_description(description):
return database.search("description", description)
search_options = {
"Part Number": search_by_part_number,
"Description": search_by_description,
}
searchBox = customtkinter.CTkComboBox(
root.midFrame2,
width=120,
height=28,
bg_color=color_5,
fg_color=color_def1,
text_color=color_def2,
values=list(search_options.keys()),
state="readonly",
)
searchBox.set("Search by:") # Set default value
searchBox.place(
x=10,
y=45,
)
####################################################################################################
# This is the Search Entry.
searchEntry = entry = customtkinter.CTkEntry(
root.midFrame2,
placeholder_text="",
placeholder_text_color=color_def2,
width=130,
height=28,
bg_color=color_5,
fg_color=color_def1,
text_color=color_def2,
)
entry.place(
x=140,
y=45,
)
####################################################################################################
# This is the Search Button.
root.searchButton = customtkinter.CTkButton(
root.midFrame2,
text_color=color_def2,
bg_color=color_5,
fg_color=color_4,
hover_color=color_def3,
# corner_radius=30,
width=100,
height=4,
border_color=color_2,
border_width=2,
cursor="hand2",
text="Search",
font=(font_3),
command=lambda: search_part_numbers(),
)
root.searchButton.place(x=30, y=80)
####################################################################################################
# This is the Search All Button.
root.showAllButton = customtkinter.CTkButton(
root.midFrame2,
text_color=color_def2,
bg_color=color_5,
fg_color=color_4,
hover_color=color_def3, # Set the hover color for the button
# corner_radius=30,
width=100,
height=4,
border_color=color_2,
border_width=2,
cursor="hand2",
text="Show All",
font=(font_3),
command=lambda: (
show_all(),
clear(True),
), # This will update the fields and then clear the text fields after button pressed.
)
root.showAllButton.place(x=140, y=80)
####################################################################################################
# This is the Excel Functions Label.
textLable = Label(
root.midFrame2,
text="Excel Functions",
font=(font_7),
bg=color_5,
fg=color_def2,
)
textLable.place(x=265, y=15)
####################################################################################################
# This is the Export to Excel Button.
root.exportLink = customtkinter.CTkButton(
root.midFrame2,
text_color=color_def2,
bg_color=color_5,
fg_color=color_4,
hover_color=color_def3, # Set the hover color for the button
# corner_radius=30,
width=100,
height=4,
border_color=color_2,
border_width=2,
cursor="hand2",
text="Export",
font=(font_3),
command=lambda: (export(), clear(True)),
)
root.exportLink.place(x=280, y=45)
####################################################################################################
# This is the Import from Excel Button.
root.importLink = customtkinter.CTkButton(
root.midFrame2,
text_color=color_def2,
bg_color=color_5,
fg_color=color_4,
hover_color=color_def3, # Set the hover color for the button
width=100,
height=4,
border_color=color_2,
border_width=2,
# corner_radius=30, # Set the corner radius for the button
cursor="hand2",
text="Import",
font=(font_3),
command=lambda: (import_data(), clear(True)),
)
root.importLink.place(x=280, y=80)
####################################################################################################
####################################################################################################
# Begin building the table. This will create a treeview for the Inventory List. ####################
####################################################################################################
####################################################################################################
# Define the treeview function.
def add_to_treeview():
part_numbers = database.fetch_inventory()
tree.delete(*tree.get_children())
for part_number in part_numbers:
tree.insert("", END, values=part_number)
def display_data_from_tree(event: None):
selected_item = tree.focus()
if selected_item:
row = tree.item(selected_item)["values"]
clear(True)
part_numberEntry.insert(0, row[0])
quantityEntry.insert(0, row[1])
descriptionEntry.insert(0, row[2])
else:
pass
####################################################################################################
# Begin treeview.
####################################################################################################
style = ttk.Style()
# Pick a theme.
style.theme_use("clam")
# Configure treeview colors.
style.configure(
"Treeview.Heading",
font=(font_4),
foreground=color_2,
background=color_5,
fieldbackground=color_3,
)
style.configure(
"Treeview",
foreground=color_def1,
fieldbackground=color_5,
background=(["selected", color_3]),
)
style.map("Treeview", background=[("selected", "green")])
tree = ttk.Treeview(
root,
height=15,
column=(
"Part Number",
"Quantity",
"Description",
),
show="headings",
)
tree.tag_configure("oddrow", background=color_3)
tree.tag_configure("evenrow", background=color_2)
# Insert parts into the treeview with alternating row colors
def add_to_treeview():
part_numbers = database.fetch_inventory()
tree.delete(*tree.get_children())
for index, part_numbers in enumerate(part_numbers):
if index % 2 == 0:
tree.insert("", END, values=part_numbers, tags=("evenrow",))
else:
tree.insert("", END, values=part_numbers, tags=("oddrow",))
tree.heading("#1", text="Part Number")
tree.heading("#2", text="Quantity")
tree.heading("#3", text="Description")
tree.column("#0", width=0, stretch=tk.NO) # Hide the default first column
tree.column("#1", width=120, anchor="nw")
tree.column("#2", width=80, anchor="center")
tree.column("#3", width=400, anchor="nw")
add_to_treeview()
####################################################################################################
####################################################################################################
# Sets the Scrollbar.
scrollbar = ttk.Scrollbar(
root,
orient=tk.VERTICAL,
command=tree.yview,
)
scrollbar.place(x=737, y=278, height=300)
tree.configure(yscrollcommand=scrollbar.set)
tree.place(
x=150,
y=250,
)
tree.bind("<ButtonRelease>", display_data_from_tree)
root.mainloop()