Sep-29-2021, 10:47 AM
Hello, me again!
I've taken on-board your advice and have begun redesigning the program. What do you think of below?
There are still some global variables and I still have to destroy and recreate some windows as I can't figure out how to dynamically update output's, such as text.
But I've redesigned the windows, renamed all the variabls to something more understandable etc the only thing I've not done is comments XD
Let me know what you think so far.
I've taken on-board your advice and have begun redesigning the program. What do you think of below?
There are still some global variables and I still have to destroy and recreate some windows as I can't figure out how to dynamically update output's, such as text.
But I've redesigned the windows, renamed all the variabls to something more understandable etc the only thing I've not done is comments XD
Let me know what you think so far.
import PySimpleGUI as sg import sqlite3 sg.theme("DarkAmber") windowlocation = [1311,0] con = sqlite3.connect(r'equipment.db') # FIGURE OUT HOW TO ACCESS SHARED DRIVE !# cur = con.cursor() filtered_equipment = [] filter_ = ("") def login(): global windowlocation admins = [["Admin","Administrator"],["Admin","OneTwoTree"]] layout0 = [[sg.T("")],[sg.T("Admin Login",font=("arial,15"))],[sg.T("Username")],[sg.InputText(key = "usr")],[sg.T("Password")],[sg.InputText(key = "pwd")],[sg.Button("OK", bind_return_key = True)],[sg.T("")],[sg.T("")],[sg.B("Guest Login")]] window0 = sg.Window("IT Portal", layout0, location=(windowlocation[0],windowlocation[1]), size = (600,500),finalize = True) event,values = window0.read() windowlocation.clear() while True: if event == sg.WIN_CLOSED: break if event == "OK": newlocation = window0.CurrentLocation() windowlocation.append(newlocation[0]) windowlocation.append(newlocation[1]) b = str(values['usr']) c = str(values['pwd']) if b in admins[0] and c in admins[1]: window0.close() adminmain() break if b not in admins or c not in admins: sg.popup('Invalid Login.','Please try again.', location = (windowlocation[0],windowlocation[1])) window0.close() login() break if event == "Guest Login": newlocation = window0.CurrentLocation() window0.close() windowlocation.append(newlocation[0]) windowlocation.append(newlocation[1]) guestmain() break def guestmain(): ticket_layout = [[sg.T("Topic:"),sg.T(" "),sg.T("Issue Description:")],[sg.Combo(values = ("Office Products","Email","Hardware","Other"),key = "topic"),sg.Multiline(size = (45,5),key = "description")], [sg.T("Email Address:")],[sg.InputText(key="email")],[sg.T("")],[sg.T("")],[sg.B("Submit Ticket"),sg.T("Please include as much information as you can.")]] equipment_request_layout = [[sg.T("Name:")],[sg.InputText(key="UserName")],[sg.T("")],[sg.T("Equipment Type:")],[sg.Combo(values = ("Monitor","Screen","Mobile","Desktop","Laptop","Mouse/Keyboard","Other"),key="Equipment_type")],[sg.T("If you selected 'Other' please specify in the box below.")],[sg.T("Please say why you need the equipment:")],[sg.Multiline(size=(45,5),key="Why")],[sg.T("")],[sg.T("")],[sg.B("Submit Request")]] window1_layout = [[sg.TabGroup([[sg.Tab('New Ticket',ticket_layout),sg.Tab('Equipment',equipment_request_layout)]])]] window1 = sg.Window('Guest Portal', window1_layout, location=(windowlocation[0],windowlocation[1]),size = (600,500), finalize = True) while True: event, values = window1.read() if event == sg.WIN_CLOSED: break if event == "Submit Ticket": topic = values["topic"] ticket_desc = values["description"] user_email = values["email"] all_tickets = [] for row in cur.execute('select * from tickets'): all_tickets.append(row) cur.execute('insert into tickets values('+str(len(all_tickets)+1) + ',"'+str(topic)+ '","' + str(ticket_desc[:-1]) + '",' + "false" + ',"' + str(user_email) + '")') con.commit() newlocation = window1.CurrentLocation() windowlocation.clear() windowlocation.append(newlocation[0]) windowlocation.append(newlocation[1]) window1.close() guestmain() break if event == "Submit Request": name = values["UserName"] equipment = values["Equipment_type"] reason = values["Why"] all_requests = [] for row in cur.execute('select * from equip_request'): all_requests.append(row) cur.execute('insert into equip_request values(' + str(len(all_requests)+1) + ',"'+str(name)+'","' + str(equipment)+'","' + str(reason[:-1])+'")') con.commit() newlocation = window1.CurrentLocation() windowlocation.clear() windowlocation.append(newlocation[0]) windowlocation.append(newlocation[1]) window1.close() guestmain() break def adminmain(): global filtered_equipment, filter_, windowlocation open_tickets = [] user_equipment = [] for row in cur.execute('select * from tickets where open_close = false'): open_tickets.append(row) for row in cur.execute('select * from user_equipment'): user_equipment.append(row) username = [] username.append(user_equipment[0][1]) x = 0 for x in range(len(user_equipment)): if user_equipment[x][1] not in username: username.append(user_equipment[x][1]) x += 1 x += 1 mobiles = 0 desktops = 0 laptops = 0 monitors = 0 for x in range(len(user_equipment)): if "Mon" in user_equipment[x][2]: monitors += 1 if "Mob" in user_equipment[x][2]: mobiles += 1 if "Des" in user_equipment[x][2]: desktops += 1 if "Lap" in user_equipment[x][2]: laptops += 1 admin_ticket_layout = [[sg.T("Open Tickets")],[sg.Listbox(values = open_tickets, size = (60,10),key="tickets"),sg.B("Select")],[sg.T(""),sg.B("Refresh")]] equipment_by_user = [[sg.T("")],[sg.T("Name")],[sg.Listbox(values=username,size = (30,10),key = "userid")],[sg.Button("Select User", bind_return_key = True)]] total_equipment = [[sg.T("Monitors"),sg.T(" "),sg.T("Desktops"),sg.T(" "),sg.T("Laptops"),sg.T(" "),sg.T("Mobiles")], [sg.T(" " +str(monitors)+ " "),sg.T(" " +str(desktops)+ " "),sg.T(" " +str(laptops)+ " "),sg.T(" " +str(mobiles))],[sg.T("")],[sg.T("")],[sg.T("")],[sg.T("")], [sg.Combo(values = ("Monitor","Desktop","Laptop","Mobile"),default_value=("Filter by"),key = "filterby"),sg.T(filter_)],[sg.Button("Filter"),sg.Listbox(values = filtered_equipment, size = (35,10))]] window2_layout = [[sg.TabGroup([[sg.Tab('Out Equipment',total_equipment),sg.Tab('Open Tickets',admin_ticket_layout),sg.Tab('Equipment',equipment_by_user)]])]] window2 = sg.Window('IT Portal', window2_layout, location=(windowlocation[0],windowlocation[1]),size = (600,500), finalize = True) while True: event,values = window2.read() if event == sg.WIN_CLOSED: break if event == "Refresh": windowlocation = [] newlocation = window2.CurrentLocation() windowlocation.append(newlocation[0]) windowlocation.append(newlocation[1]) window2.close() adminmain() if event == "Filter": filtered_equipment.clear() filter_ = values["filterby"] for x in range(len(user_equipment)): if filter_ in user_equipment[x]: filtered_equipment.append(user_equipment[x][1]) filtered_equipment.append(user_equipment[x][3]) x += 1 windowlocation = [] newlocation = window2.CurrentLocation() windowlocation.append(newlocation[0]) windowlocation.append(newlocation[1]) window2.close() adminmain() break if event == "Select": windowlocation = [] newlocation = window2.CurrentLocation() windowlocation.append(newlocation[0]) windowlocation.append(newlocation[1]) selected_ticket = values["tickets"] if len(selected_ticket) < 1: sg.popup("Invalid Selection","Please Choose A Ticket.", location = (windowlocation[0],windowlocation[1])) adminmain() break notes = [[]] for row in cur.execute('select * from notes where id =' + str(selected_ticket[0][0]) + ';'): notes.append(row) if len(notes) == 1: for x in range(4): notes[0].append("NA") selection = [[sg.T("")],[sg.T("ID: "),sg.T(selected_ticket[0][0])],[sg.T("Topic: "),sg.T(selected_ticket[0][1])],[sg.T("Issue: "),sg.T(selected_ticket[0][2])],[sg.T("Name: "),sg.T(selected_ticket[0][4])],[sg.B("Clear")],[sg.T(notes[len(notes)-1][-1])],[sg.T(notes[len(notes)-1][-2])],[sg.T("")],[sg.B("New Note")]] window4 = sg.Window("IT Admin portal - View Ticket", selection,location=(windowlocation[0],windowlocation[1]), size = (300,300), finalize = True) while True: event,values = window4.read() if event == sg.WIN_CLOSED: window2.close() adminmain() break if event == "Clear": cur.execute('update tickets set open_close = True where id =' + str(selected_ticket[0][0]) + ';') con.commit() selected_ticket.clear() window4.close() window2.close() adminmain() if event == "New Note": windowlocation = [] newlocation = window2.CurrentLocation() windowlocation.append(newlocation[0])Ad windowlocation.append(newlocation[1]) new_note_layout = [[sg.T("Date: "),sg.InputText(key = "notedate")],[sg.T("Note: "),sg.InputText(key = "notetxt")],[sg.B("OK")]] window5 = sg.Window('Note Taker', new_note_layout, size = (200,100)) while True: event, values = window5.read() if event == sg.WIN_CLOSED: break if event == "OK": notedate = values['notedate'] notetxt = values['notetxt'] cur.execute('insert into notes values('+str(selected_ticket[0][0])+',"'+str(notetxt)+'","'+str(notedate)+'")') con.commit() window5.close() window4.close() window2.close() adminmain() break if event == "Select User": windowlocation = [] newlocation = window2.CurrentLocation() windowlocation.append(newlocation[0]) windowlocation.append(newlocation[1]) user_id = str(values["userid"]) equip = [] serial = [] if len(user_id) < 3: sg.popup("Invalid Selection","Please Choose From The List",location = (windowlocation[0],windowlocation[1])) window2.close() adminmain() break for x in range(len(user_equipment)): if user_id[2:-2] in user_equipment[x][1]: equip.append(user_equipment[x][2]) serial.append(user_equipment[x][3]) x += 1 user_equipment_details = [[sg.T("Type"),sg.T(" "),sg.T("Serial"),sg.T(" "),sg.B("Back")],[sg.Listbox(values=equip,size = (10,10),key = "user_equip"),sg.Listbox(values=serial,size=(33,10),key = "serial")],[sg.T("")],[sg.T("")], [sg.T("An equipment return form must be filled out, scanned and saved.")],[sg.T("Select the serial number of the returned item.")],[sg.Button("Return")]] window3 = sg.Window('IT Admin Portal - User Details', user_equipment_details, location = (windowlocation[0],windowlocation[1]), size = (600,500), finalize = True) while True: event,values = window3.read() if event == sg.WIN_CLOSED: window2.close() adminmain() break if event == "Back": windowlocation = [] newlocation = window2.CurrentLocation() windowlocation.append(newlocation[0]) windowlocation.append(newlocation[1]) window3.close() window2.close() adminmain() if event == "Return": chosen_serial = values["serial"] cur.execute('delete from user_equipment where serial ="' + str(chosen_serial)+'";') con.commit() window3.close() window2.close() adminmain() break login()
while dad_has_cigs == True: happiness = True if dad_has_cigs == False: print("Dad come home!") happiness = not happiness break