ComputerKeeper/main.py
Josh b 801d3b4b1d
All checks were successful
Potlatch Loggers Organization/ComputerKeeper/pipeline/head This commit looks good
enabled it to remove things
2022-09-25 19:15:15 -07:00

86 lines
2.7 KiB
Python

"""This is the main function"""
from copy import deepcopy
import PySimpleGUI as sg
from tinydb import TinyDB, Query
# Define the window's contents
colum_lut = ["Serial Number","Brand","Grade","Name"]
layout = [[sg.Menu([['File', ['Open', 'Save', 'Add Computer','Remove Computer']]])],
[sg.Table([["word","Yes"]],colum_lut,key = "table")],
[sg.Text("Serial Number")],
[sg.Input(key='Serial Number')],
[sg.Text("Brand")],
[sg.Input(key='Brand')],
[sg.Text("Grade")],
[sg.Input(key='Grade')],
[sg.Text("Name")],
[sg.Input(key='Name')],
[sg.Text(size=(40,1), key='OUTPUT')],
[sg.Button('Check In'), sg.Button('Check Out')]]
add_computer_layout = [[sg.Text("Serial Number")],
[sg.Input(key='Serial Number')],
[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'),]]
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
def main():
"""Core of ComputerKeeper - The Main Application."""
db= TinyDB("./db.json")
Computer = []
for item in db.all():
The_Device = []
for colum in colum_lut:
The_Device.append(item[colum])
Computer.append(The_Device)
# Create the window
window = sg.Window('Window Title', layout, finalize=True)
window["table"].update(Computer)
# 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
if event == 'Add Computer':
new_computer = add_computer()
existing_table = window["table"].get()
existing_table.append( list(new_computer.values()))
window["table"].update(existing_table)
db.insert(new_computer)
elif event == 'Remove Computer':
if values["table"]:
existing_table = window["table"].get()
serial = existing_table.pop(values["table"][0])[0]
query = Query()
db.remove(query["Serial Number"] == serial)
window["table"].update(existing_table)
# Finish up by removing from the screen
window.close()
if __name__ == "__main__":
main()