ComputerKeeper/main.py

149 lines
5.3 KiB
Python
Raw Normal View History

2022-08-19 02:17:43 +00:00
"""This is the main function"""
2022-08-26 02:27:35 +00:00
from copy import deepcopy
2022-08-19 02:17:43 +00:00
import PySimpleGUI as sg
2022-08-26 02:27:35 +00:00
from tinydb import TinyDB, Query
2022-11-07 02:57:53 +00:00
from logo_source import COMPUTER_KEEPER_LOGO
2022-08-19 02:17:43 +00:00
2022-09-30 00:57:22 +00:00
TABLE=None
WINDOW=None
2022-08-19 02:17:43 +00:00
# Define the window's contents
2022-09-09 00:44:20 +00:00
colum_lut = ["Serial Number","Brand","Grade","Name"]
layout = [[sg.Menu([['File', ['Open', 'Save', 'Add Computer','Remove Computer']]])],
2022-10-22 00:49:05 +00:00
[sg.Text("search"), sg.Input(s=(35,), enable_events=True,key="search")],
2022-10-14 00:58:26 +00:00
[sg.Input(s=(19,),enable_events=True,key="filter Serial Number"),
sg.Input(s=(6,),enable_events=True,key="filter Brand"),
sg.Input(s=(6,),enable_events=True,key="filter Grade"),
sg.Input(s=(4,),enable_events=True,key="filter Name")],
2022-11-11 01:24:34 +00:00
[sg.Table([["word","Yes"]],colum_lut,key = "table", enable_events=True, expand_x = True, expand_y =True)],
2022-08-26 01:10:43 +00:00
[sg.Text("Serial Number")],
2022-08-26 02:27:35 +00:00
[sg.Input(key='Serial Number')],
2022-08-26 01:10:43 +00:00
[sg.Text("Brand")],
2022-08-26 02:27:35 +00:00
[sg.Input(key='Brand')],
2022-08-26 01:10:43 +00:00
[sg.Text("Grade")],
2022-08-26 02:27:35 +00:00
[sg.Input(key='Grade')],
2022-08-26 01:10:43 +00:00
[sg.Text("Name")],
2022-08-26 02:27:35 +00:00
[sg.Input(key='Name')],
[sg.Text(size=(40,1), key='OUTPUT')],
2022-08-26 01:10:43 +00:00
[sg.Button('Check In'), sg.Button('Check Out')]]
2022-08-19 02:17:43 +00:00
2022-08-26 02:27:35 +00:00
add_computer_layout = [[sg.Text("Serial Number")],
2022-09-07 00:23:49 +00:00
[sg.Input(key='Serial Number')],
2022-08-26 02:27:35 +00:00
[sg.Text("Brand")],
[sg.Input(key='Brand')],
[sg.Text("Grade")],
[sg.Input(key='Grade')],
[sg.Text("Name")],
[sg.Input(key='Name')],
[sg.Button('Add Computer'),]]
2022-08-19 02:17:43 +00:00
2022-09-30 00:57:22 +00:00
def double_click(event):
"""
Additional event for double-click on header
event: class event
"""
region = TABLE.identify("region", event.x, event.y)
if region == 'heading': # Only care double-clock on headings
cid = int(TABLE.identify_column(event.x)[1:])-1 # check which column clicked
WINDOW.write_event_value("-TABLE-DOUBLE-CLICK-", cid)
2022-08-19 02:17:43 +00:00
2022-08-26 02:27:35 +00:00
def add_computer():
window = sg.Window('Add Computer', deepcopy(add_computer_layout))
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED or event == 'Add Computer':
break
# Finish up by removing from the screen
window.close()
return values
2022-08-19 02:17:43 +00:00
2022-10-22 00:20:57 +00:00
def format_table(table_list):
2022-09-07 01:02:03 +00:00
Computer = []
2022-10-22 00:20:57 +00:00
for item in table_list:
2022-09-07 01:02:03 +00:00
The_Device = []
for colum in colum_lut:
The_Device.append(item[colum])
Computer.append(The_Device)
2022-10-22 00:20:57 +00:00
return Computer
def main():
"""Core of ComputerKeeper - The Main Application."""
global TABLE,WINDOW
db= TinyDB("./db.json")
computer = format_table(db.all())
2022-08-19 02:17:43 +00:00
# Create the window
2022-11-11 01:24:34 +00:00
window = sg.Window('Computer Keeper', layout, finalize=True, icon=COMPUTER_KEEPER_LOGO, resizable = True)
2022-09-30 00:57:22 +00:00
WINDOW=window
2022-10-22 00:20:57 +00:00
window["table"].update(computer)
2022-09-30 00:57:22 +00:00
table = window['table'].Widget
table.bind('<Double-1>', double_click, add='+')
TABLE=table
2022-08-19 02:17:43 +00:00
# Display and interact with the Window using an Event Loop
while True:
event, values = window.read()
# See if user wants to quit or window was closed
if event == sg.WINDOW_CLOSED or event == 'Quit':
break
2022-08-26 02:27:35 +00:00
if event == 'Add Computer':
new_computer = add_computer()
existing_table = window["table"].get()
2022-09-05 01:57:27 +00:00
existing_table.append( list(new_computer.values()))
2022-08-26 02:27:35 +00:00
window["table"].update(existing_table)
2022-09-05 01:57:27 +00:00
db.insert(new_computer)
2022-09-09 00:44:20 +00:00
elif event == 'Remove Computer':
if values["table"]:
existing_table = window["table"].get()
2022-09-26 02:15:15 +00:00
serial = existing_table.pop(values["table"][0])[0]
query = Query()
db.remove(query["Serial Number"] == serial)
2022-09-09 00:44:20 +00:00
window["table"].update(existing_table)
2022-09-30 00:57:22 +00:00
elif event == '-TABLE-DOUBLE-CLICK-':
column = values[event]
print(f'Click on column {column}')
# Sort data on the table by the value of column
# then update window['-TABLE-'].update(values=new_data)
2022-10-22 00:20:57 +00:00
elif event.find("filter") != -1:
2022-10-14 01:08:29 +00:00
filter_database = lambda s: s.lower().find(values[event].lower()) != -1
2022-10-14 00:58:26 +00:00
Computers=Query()
2022-10-22 00:20:57 +00:00
result=db.search(Computers[event[7:]].test(filter_database))
window["table"].update(format_table(result))
2022-09-30 00:57:22 +00:00
2022-10-22 00:49:05 +00:00
elif event == "search":
filter_database = lambda s: s.lower().find(values[event].lower()) != -1
Computers=Query()
result=db.search(
Computers["Serial Number"].test(filter_database)|
Computers["Brand"].test(filter_database)|
Computers["Grade"].test(filter_database)|
Computers["Name"].test(filter_database)
)
window["table"].update(format_table(result))
2022-08-19 02:17:43 +00:00
2022-11-11 01:41:27 +00:00
elif event == "table" and values["table"]:
2022-10-28 00:54:30 +00:00
row=window["table"].get()[values["table"][0]]
window["Serial Number"].update(row[0])
window["Brand"].update(row[1])
window["Grade"].update(row[2])
window["Name"].update(row[3])
2022-11-11 01:52:40 +00:00
elif event == "Check Out":
SN = window["Serial Number"].get()
G = window["Grade"].get()
N = window["Name"].get()
Table = window["table"].get()
print(Table)
2022-08-19 02:17:43 +00:00
# Finish up by removing from the screen
window.close()
if __name__ == "__main__":
main()