Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Equipment Portal
#1
Hello,

Please see my finished code below, I'm looking for suggestions of features to add and how to make it better :)

import PySimpleGUI as sg
import csv
from csv import reader
import sqlite3
sg.theme("DarkAmber")
entry = []
equipm = []
a = ""
t = [810,390]
p = ("")
admins = [["Admin","Administrator"],["Admin","OneTwoTree"]]
con = sqlite3.connect(r'equipment.db') # FIGURE OUT HOW TO ACCESS SHARED DRIVE !#
cur = con.cursor()
def startup():        
    for row in cur.execute('SELECT * FROM user_equipment'):
        entry.append(row)
    login()
def login():
    global b
    layout0 = [[sg.T("")],[sg.T("Username")],[sg.InputText(key = "usr")],[sg.T("Password")],[sg.InputText(key = "pwd")],[sg.Button("OK", bind_return_key = True)]]
    window0 = sg.Window("Equipment Portal", layout0, location=(t[0],t[1]), size = (600,500),finalize = True)
    event,values = window0.read()
    t.clear()
    while True:
        if event == sg.WIN_CLOSED:
            break
        if event == "OK":
            u = window0.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            b = str(values['usr'])
            c = str(values['pwd'])
            if b in admins[0] and c in admins[1]:
                window0.close()
                main()                
                break
            if b not in admins or c not in admins:
                sg.popup('Invalid Login.','Please try again.', location = (t[0],t[1]))
                window0.close()
                login()
                break
def main():
    layout1 = [[sg.T("")],[sg.Button("Out Equipment"),sg.Button("User List"),sg.Button("New Entry")]]
    window1 = sg.Window("Equipment Portal", layout1,location=(t[0],t[1]), size = (600,500), finalize = True)
    event, values = window1.read()
    t.clear()
    while True:
        if event == sg.WIN_CLOSED:
            con.close()
            break
        if event == "User List":
            u = window1.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window1.close()
            user_list()
            break
        if event == "Out Equipment":            
            u = window1.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window1.close()
            out_equipment()
            break
        if event == "New Entry":
            u = window1.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window1.close()
            new_entry()
            break
def user_list():
    entry.clear()
    for row in cur.execute('select * from user_equipment'):
            entry.append(row)
    user = []
    user.append(entry[0][1])
    x = 0
    global a
    for x in range(len(entry)):        
        if entry[x][1] not in user:
            user.append(entry[x][1])
            x += 1  
        x += 1        
    user_list1 = [[sg.T("")],[sg.Button("Out Equipment"),sg.Button("User List"),sg.Button("New Entry")],[sg.T("")],
                        [sg.Listbox(values=user,size = (30,10),key = "userid")],[sg.Button("Select", bind_return_key = True)]]
    window2 = sg.Window("Equipment Portal", user_list1,location=(t[0],t[1]), size = (600,500), finalize = True)
    event, values = window2.read()
    t.clear()
    while True:
            if event == sg.WIN_CLOSED:
                con.close()
                break
            if event == "User List":
                u = window2.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window2.close()
                user_list()
                break
            if event == "Out Equipment":            
                u = window2.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window2.close()
                out_equipment()
                break
            if event == "New Entry":
                u = window2.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window2.close()
                new_entry()
                break
            if event == "Select":
                a = str(values['userid'])
                u = window2.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window2.close()
                user_equip()
                break
def user_equip():
    equip = []
    serial = []
    x = 0
    global a
    for x in range(len(entry)):
        if a[2:5] in entry[x][1]:
            equip.append(entry[x][2])
            serial.append(entry[x][3])
            x += 1    
    user_list2 = [[sg.T("")],[sg.Button("Out Equipment"),sg.Button("User List"),sg.Button("New Entry")],[sg.T(a)],[sg.T("Type"),sg.T("           "),sg.T("Serial")],
                    [sg.Listbox(values=equip,size = (10,10),key = "user_equip"),sg.Listbox(values=serial,size=(33,10),key = "D")],[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("Equipment Portal", user_list2,location=(t[0],t[1]), size = (600,500), finalize = True)
    event, values = window3.read()
    t.clear()
    while True:
            if event == sg.WIN_CLOSED:
                con.close()
                break
            if event == "User List":
                u = window3.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window3.close() 
                user_list()
                break
            if event == "Out Equipment":    
                u = window3.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window3.close()
                out_equipment()
                break
            if event == "New Entry":
                u = window3.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window3.close()
                new_entry()
                break
            if event == "Return":
                entry.clear()
                for row in cur.execute('select * from user_equipment'):
                        entry.append(row)
                z = str(values["D"])  
                u = window3.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                for y in range(len(entry)):
                    if z[2:-2] == entry[y][3]:
                        cur.execute('delete from user_equipment where serial="'+str(entry[y][3])+'";')                  
                        con.commit()
                window3.close()
                entry.clear()
                login()                
                break                        
def new_entry():
    new_ntr = [[sg.T("")],[sg.Button("Out Equipment"),sg.Button("User List"),sg.Button("New Entry")],[sg.T("")],
                    [sg.T("Name")],[sg.InputText(key = "A")],[sg.T("Equipment Type")],[sg.Combo(values = ("Monitor","Desktop","Laptop","Mobile"),size=(15,15),key = "B")],[sg.T("Serial")],[sg.InputText(key = "C")],[sg.T("")],[sg.T("")],
               [sg.T("")],[sg.T("An issue form must also be filled out, scanned, and saved.")],[sg.Button("Save", bind_return_key = True)]]
    window5 = sg.Window("Equipment Portal", new_ntr,location = (t[0],t[1]), size = (600,500), finalize = True)
    event, values = window5.read()
    t.clear()
    while True:
            if event == sg.WIN_CLOSED:
                con.close()
                break
            if event == "User List":
                u = window5.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window5.close()
                user_list()
                break
            if event == "Out Equipment":            
                u = window5.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window5.close()
                out_equipment()
                break
            if event == "New Entry":
                u = window5.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window5.close()
                new_entry()
                break
            if event == "Save":
                entry.clear()
                for row in cur.execute('select * from user_equipment'):
                        entry.append(row)
                x = values["A"]
                y = values["B"]
                z = values["C"]
                a = int(len(entry)+1)
                cur.execute('insert into user_equipment values('+ str(a) + ',"' + x + '","' + y + '","' + z + '");')
                con.commit()
                entry.clear()
                u = window5.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window5.close()
                startup()
                break
def out_equipment():
    entry.clear()
    for row in cur.execute('select * from user_equipment'):
        entry.append(row)
    mob = 0
    des = 0
    lap = 0
    mon = 0
    global p
    global equipm
    equip = []
    for x in range(len(entry)):
        if "Mon" in entry[x][2]:
            mon += 1
        if "Des" in entry[x][2]:
            des += 1
        if "Lap" in entry[x][2]:
            lap += 1
        if "Mob" in entry[x][2]:
            mob += 1    
    out_equip = [[sg.T("")],[sg.Button("Out Equipment"),sg.Button("User List"),sg.Button("New Entry")],[sg.T("")],
                   [sg.T("Monitors"),sg.T("       "),sg.T("Desktops"),sg.T("       "),sg.T("Laptops"),sg.T("       "),sg.T("Mobiles")],
                 [sg.T("     " +str(mon)+ "         "),sg.T("             " +str(des)+ "           "),sg.T("          " +str(lap)+ "   "),sg.T("                  " +str(mob))],[sg.T("")],[sg.T("")],[sg.T("")],[sg.T("")],
                 [sg.Combo(values = ("Monitor","Desktop","Laptop","Mobile"),default_value=("Filter by"),key = "filterby"),sg.T(p)],[sg.Button("Filter"),sg.Listbox(values = equipm, size = (35,10))]]
    window4 = sg.Window("Equipment Portal", out_equip,location=(t[0],t[1]), size = (600,500), finalize = True)
    event, values = window4.read()
    t.clear()
    while True:
            if event == sg.WIN_CLOSED:
                con.close()
                break
            if event == "User List":
                u = window4.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window4.close()
                user_list()
                break
            if event == "Out Equipment":
                u = window4.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window4.close()
                out_equipment()
                break
            if event == "New Entry":
                u = window4.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window4.close()
                new_entry()
                break
            if event == "Filter":
                equipm = []
                z = values["filterby"]
                p = values["filterby"]
                for x in range(len(entry)):
                    if z in entry[x]:
                        equipm.append(entry[x][1])
                        equipm.append(entry[x][3])
                    x += 1
                u = window4.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window4.close()
                out_equipment()
                break
startup()
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply
#2
It's unclear what your code is supposed to do and in any case we can comment on your code/implementation and we can hardly suggest new features. That's why I will comment on your signature.
  • dad_has_cigs is obviously a bool. Comparing it to True and False is redundant. while dad_has_cigs: and if not dad_has_cigs: is enough.
  • If dad_has_cigs is True and you enter the loop, there is no was that consecutive if dad_has_cigs == False: condition to be evaluated True and execute the body. You never change the value of dad_has_cigs so it will never be executed, unless it's considered to be global variable and changed independently in different thread (i.e. dad smokes in a different threat).
  • Indentation is unclear, but I guess last 3 lines are indented.
  • I hope this comment will not be taken too seriously, but some lessons will be learnt :-)


And some comments on your "main" code
  • Cryptic [one-letter] names.
  • No comments
  • Mix of GUI and logic
  • heavy use of global variables
  • repetitive code, in need of refactoring
I guess it's clear all of the above is considered bad practice.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hello,

I get comments on that all the time, it was supposed to be a joke more than anything :')

With regards to the code, I know it's pooooooorly written, I normally do a messy version that works and then rework it once that's done. The lack of comments is understandable, I never expect anyone else to read the code so I rarely do them. Not an excuse, just a fact.

The mix of GUI and logic is on purpose.

Single letter variables are again for time saving.

The globals are unfortunately needed, as the program needs to pass data between functions and I'm not savvy enough with OOP to be able to do it that way.

The second version (More like the fourth version tbh) is below, I've added a lot of features and yes, it is still very messy, but it works as intended. Although I've been told that in PySimpleGUI you should have the "event,values=window.read()" inside the while loop, I'm wondering why that is?

import PySimpleGUI as sg
import sqlite3
sg.theme("DarkAmber")
entry = []
equipm = []
a = ""
ti = []
t = [1311,0]
p = ("")
xy = "NA"
yz = "NA"
zx = "NA"
admins = [["Admin","Administrator"],["Admin","OneTwoTree"]]
con = sqlite3.connect(r'equipment.db') # FIGURE OUT HOW TO ACCESS SHARED DRIVE !#
cur = con.cursor()
def startup():        
    for row in cur.execute('SELECT * FROM user_equipment'):
        entry.append(row)
    login()
def login():
    global b
    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=(t[0],t[1]), size = (600,500),finalize = True)
    event,values = window0.read()
    t.clear()
    while True:
        if event == sg.WIN_CLOSED:
            break
        if event == "OK":
            u = window0.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            b = str(values['usr'])
            c = str(values['pwd'])
            if b in admins[0] and c in admins[1]:
                window0.close()
                main()                
                break
            if b not in admins or c not in admins:
                sg.popup('Invalid Login.','Please try again.', location = (t[0],t[1]))
                window0.close()
                login()
                break
        if event == "Guest Login":
            u = window0.CurrentLocation()
            window0.close()
            t.append(u[0])
            t.append(u[1])
            guestmain()
            break
#########################################################################GUEST SECTION#########################################################################
def guestmain():
    layout10 = [[sg.T("")],[sg.B("Request Equipment"),sg.B("Raise Ticket")]]
    window10 = sg.Window("Guest Portal", layout10,location = (t[0],t[1]), size = (600,500),finalize = True)
    event, values = window10.read()
    t.clear()
    while True:
        if event == sg.WIN_CLOSED:
            con.close()
            break
        if event == "Request Equipment":
            u = window10.CurrentLocation()
            window10.close()
            t.append(u[0])
            t.append(u[1])
            equipreq()
            break
        if event == "Raise Ticket":
            u = window10.CurrentLocation()
            window10.close()
            t.append(u[0])
            t.append(u[1])
            newticket()
            break
def newticket():
    layout11 = [[sg.T("")],[sg.B("Request Equipment"),sg.B("Raise Ticket")],[sg.T("")],[sg.T("Topic:"),sg.T("                    "),sg.T("Issue Description:")],[sg.Combo(values = ("Office Products","Other"),key = "D"),sg.Multiline(size = (45,5),key = "E")],
                [sg.T("Email Address:")],[sg.InputText(key="F")],[sg.T("")],[sg.T("")],[sg.B("Submit Ticket"),sg.T("Please include as much information as you can.")]]
    window11 = sg.Window("Guest Portal",layout11,location=(t[0],t[1]),size=(600,500), finalize = True)
    event,values = window11.read()
    t.clear()
    while True:
        if event == sg.WIN_CLOSED:
            con.close()
            break
        if event == "Request Equipment":
            u = window11.CurrentLocation()
            window11.close()
            t.append(u[0])
            t.append(u[1])
            equipreq()
            break
        if event == "Raise Ticket":
            u = window11.CurrentLocation()
            window11.close()
            t.append(u[0])
            t.append(u[1])
            newticket()
            break
        if event == "Submit Ticket":
            x = values["D"]
            y = values["E"]
            z = values["F"]
            ti = []
            for row in cur.execute('select * from tickets'):
                ti.append(row)
            cur.execute('INSERT INTO tickets values('+ str(len(ti)+1) +',"'+ str(x) + '","' + str(y[:-1])+ '",' + "false" + ',"' + str(z) + '")')
            con.commit()
            u = window11.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window11.close()
            newticket()
            break
def equipreq():
    layout12 = [[sg.T("")],[sg.B("Request Equipment"),sg.B("Raise Ticket")],[sg.T("")],[sg.T("Name:")],[sg.InputText(key="G")],[sg.T("")],[sg.T("Equipment Type:")],[sg.Combo(values = ("Monitor","Screen","Mobile","Desktop","Laptop","Mouse/Keyboard","Other"),key="H")],[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="I")],[sg.T("")],[sg.T("")],[sg.B("Submit")]]
    window12 = sg.Window("Guest Portal", layout12,location = (t[0],t[1]), size = (600,500), finalize = True)
    event,values = window12.read()
    t.clear()
    while True:
        if event == sg.WIN_CLOSED:
            con.close()
            break
        if event == "Request Equipment":
            u = window12.CurrentLocation()
            window12.close()
            t.append(u[0])
            t.append(u[1])
            equipreq()
            break
        if event == "Raise Ticket":
            u = window12.CurrentLocation()
            window12.close()
            t.append(u[0])
            t.append(u[1])
            newticket()
            break
        if event == "Submit":
            x = values["G"]
            y = values["H"]
            z = values["I"]
            ti = []
            for row in cur.execute('select * from equip_request'):
                ti.append(row)
            cur.execute('INSERT INTO equip_request values('+ str(len(ti)+1) + ',"' + str(x) + '","' + str(y) + '","' + str(z[:-1])+'")')
            con.commit()
            u = window12.CurrentLocation()
            window12.close()
            t.append(u[0])
            t.append(u[1])
            equipreq()
            break
#########################################################################END OF GUEST SECTION#########################################################################
#########################################################################ADMIN SECTION#########################################################################
def main():
    layout1 = [[sg.T("")],[sg.Button("View Logged Equipment"),sg.Button("User List"),sg.Button("Log Equipment Out"),sg.B("Open Tickets"),sg.B("Open Requests")]]
    window1 = sg.Window("IT Admin Portal", layout1,location=(t[0],t[1]), size = (600,500), finalize = True)
    event, values = window1.read()
    t.clear()
    while True:
        if event == sg.WIN_CLOSED:
            con.close()
            break
        if event == "User List":
            u = window1.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window1.close()
            user_list()
            break
        if event == "View Logged Equipment":            
            u = window1.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window1.close()
            out_equipment()
            break
        if event == "Log Equipment Out":
            u = window1.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window1.close()
            new_entry()
            break
        if event == "Open Tickets":
            u = window1.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window1.close()
            tickets()
            break
        if event == "Open Requests":
            u = window1.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window1.close()
            xy = "NA"
            yz = "NA"
            zx = "NA"
            requestlist()
            break
def user_list():
    entry.clear()
    for row in cur.execute('select * from user_equipment'):
            entry.append(row)
    user = []
    user.append(entry[0][1])
    x = 0
    global a
    for x in range(len(entry)):        
        if entry[x][1] not in user:
            user.append(entry[x][1])
            x += 1  
        x += 1        
    user_list1 = [[sg.T("")],[sg.Button("View Logged Equipment"),sg.Button("User List"),sg.Button("Log Equipment Out"),sg.B("Open Tickets"),sg.B("Open Requests")],[sg.T("")],[sg.T("Name")],
                        [sg.Listbox(values=user,size = (30,10),key = "userid")],[sg.Button("Select", bind_return_key = True)]]
    window2 = sg.Window("IT Admin Portal - User List", user_list1,location=(t[0],t[1]), size = (600,500), finalize = True)
    event, values = window2.read()
    t.clear()
    while True:
            if event == sg.WIN_CLOSED:
                con.close()
                break
            if event == "User List":
                u = window2.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window2.close()
                user_list()
                break
            if event == "View Logged Equipment":            
                u = window2.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window2.close()
                out_equipment()
                break
            if event == "Open Tickets":            
                u = window2.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window2.close()
                tickets()
                break
            if event == "Log Equipment Out":
                u = window2.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window2.close()
                new_entry()
                break
            if event == "Select":
                a = str(values['userid'])
                u = window2.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                if len(a) < 3:
                    sg.popup("Invalid Selection","Please Choose From The List",location = (t[0],t[1]))
                    window2.close()
                    user_list()
                window2.close()
                user_equip()
                break
            if event == "Open Requests":
                u = window2.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window2.close()
                xy = "NA"
                yz = "NA"
                zx = "NA"
                requestlist()
                break
def user_equip():
    equip = []
    serial = []
    x = 0
    entry.clear()
    for row in cur.execute('select * from user_equipment'):
            entry.append(row)
    global a
    for x in range(len(entry)):
        if a[2:5] in entry[x][1]:
            equip.append(entry[x][2])
            serial.append(entry[x][3])
            x += 1    
    user_list2 = [[sg.T("")],[sg.Button("View Logged Equipment"),sg.Button("User List"),sg.Button("Log Equipment Out"),sg.B("Open Tickets"),sg.B("Open Requests")],[sg.T(a)],[sg.T("Type"),sg.T("           "),sg.T("Serial")],
                    [sg.Listbox(values=equip,size = (10,10),key = "user_equip"),sg.Listbox(values=serial,size=(33,10),key = "D")],[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_list2,location=(t[0],t[1]), size = (600,500), finalize = True)
    event, values = window3.read()
    t.clear()
    while True:
            if event == sg.WIN_CLOSED:
                con.close()
                break
            if event == "User List":
                u = window3.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window3.close() 
                user_list()
                break
            if event == "View Logged Equipment":    
                u = window3.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window3.close()
                out_equipment()
                break
            if event == "Log Equipment Out":
                u = window3.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window3.close()
                new_entry()
                break
            if event == "Open Tickets":
                u = window3.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window3.close()
                tickets()
                break
            if event == "Return":
                entry.clear()
                for row in cur.execute('select * from user_equipment'):
                        entry.append(row)
                z = str(values["D"])  
                u = window3.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                for y in range(len(entry)):
                    if z[2:-2] == entry[y][3]:
                        cur.execute('delete from user_equipment where serial="'+str(entry[y][3])+'";')                  
                        con.commit()
                window3.close()
                entry.clear()
                login()                
                break
            if event == "Open Requests":
                u = window3.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window3.close()
                xy = "NA"
                yz = "NA"
                zx = "NA"
                requestlist()
                break
                
def new_entry():
    new_ntr = [[sg.T("")],[sg.Button("View Logged Equipment"),sg.Button("User List"),sg.Button("Log Equipment Out"),sg.B("Open Tickets"),sg.B("Open Requests")],[sg.T("")],
                    [sg.T("Name")],[sg.InputText(key = "A")],[sg.T("Equipment Type")],[sg.Combo(values = ("Monitor","Desktop","Laptop","Mobile"),size=(15,15),key = "B")],[sg.T("Serial")],[sg.InputText(key = "C")],[sg.T("")],[sg.T("")],
               [sg.T("")],[sg.T("An issue form must also be filled out, scanned, and saved.")],[sg.Button("Save", bind_return_key = True)]]
    window5 = sg.Window("IT Admin Portal - Log Equipment Out", new_ntr,location = (t[0],t[1]), size = (600,500), finalize = True)
    event, values = window5.read()
    t.clear()
    while True:
            if event == sg.WIN_CLOSED:
                con.close()
                break
            if event == "User List":
                u = window5.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window5.close()
                user_list()
                break
            if event == "View Logged Equipment":            
                u = window5.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window5.close()
                out_equipment()
                break
            if event == "Log Equipment Out":
                u = window5.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window5.close()
                new_entry()
                break
            if event == "Open Tickets":
                u = window5.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window5.close()
                tickets()
                break
            if event == "Save":
                entry.clear()
                for row in cur.execute('select * from user_equipment'):
                        entry.append(row)
                x = values["A"]
                y = values["B"]
                z = values["C"]
                a = int(len(entry)+1)
                cur.execute('insert into user_equipment values('+ str(a) + ',"' + x + '","' + y + '","' + z + '");')
                con.commit()
                entry.clear()
                u = window5.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window5.close()
                startup()
                break
            if event == "Open Requests":
                u = window5.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window5.close()
                xy = "NA"
                yz = "NA"
                zx = "NA"
                requestlist()
                break
def out_equipment():
    entry.clear()
    for row in cur.execute('select * from user_equipment'):
        entry.append(row)
    mob = 0
    des = 0
    lap = 0
    mon = 0
    global p
    global equipm
    equip = []
    for x in range(len(entry)):
        if "Mon" in entry[x][2]:
            mon += 1
        if "Des" in entry[x][2]:
            des += 1
        if "Lap" in entry[x][2]:
            lap += 1
        if "Mob" in entry[x][2]:
            mob += 1    
    out_equip = [[sg.T("")],[sg.Button("View Logged Equipment"),sg.Button("User List"),sg.Button("Log Equipment Out"),sg.B("Open Tickets"),sg.B("Open Requests")],[sg.T("")],
                   [sg.T("Monitors"),sg.T("       "),sg.T("Desktops"),sg.T("       "),sg.T("Laptops"),sg.T("       "),sg.T("Mobiles")],
                 [sg.T("     " +str(mon)+ "         "),sg.T("             " +str(des)+ "           "),sg.T("          " +str(lap)+ "   "),sg.T("                  " +str(mob))],[sg.T("")],[sg.T("")],[sg.T("")],[sg.T("")],
                 [sg.Combo(values = ("Monitor","Desktop","Laptop","Mobile"),default_value=("Filter by"),key = "filterby"),sg.T(p)],[sg.Button("Filter"),sg.Listbox(values = equipm, size = (35,10))]]
    window4 = sg.Window("IT Admin Portal - View Logged Equipment", out_equip,location=(t[0],t[1]), size = (600,500), finalize = True)
    event, values = window4.read()
    t.clear()
    while True:
            if event == sg.WIN_CLOSED:
                con.close()
                break
            if event == "User List":
                u = window4.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window4.close()
                user_list()
                break
            if event == "View Logged Equipment":
                u = window4.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window4.close()
                out_equipment()
                break
            if event == "Log Equipment Out":
                u = window4.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window4.close()
                new_entry()
                break
            if event == "Filter":
                equipm = []
                z = values["filterby"]
                p = values["filterby"]
                for x in range(len(entry)):
                    if z in entry[x]:
                        equipm.append(entry[x][1])
                        equipm.append(entry[x][3])
                    x += 1
                u = window4.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window4.close()
                out_equipment()
                break
            if event == "Open Tickets":
                u = window4.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window4.close()
                tickets()
                break
            if event == "Open Requests":
                u = window4.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window4.close()
                xy = "NA"
                yz = "NA"
                zx = "NA"
                requestlist()
                break
def tickets():
    global ti
    ti = []
    for row in cur.execute('select * from tickets where open_close = false'):
        ti.append(row)
    layout12 = [[sg.T("")],[sg.Button("View Logged Equipment"),sg.Button("User List"),sg.Button("Log Equipment Out"),sg.B("Open Tickets"),sg.B("Open Requests")],[sg.T("")],[sg.T("Open Tickets")],[sg.Listbox(values = ti, size = (60,10),key="J"),sg.B("Select")]]
    window12 = sg.Window("IT Admin Portal - Open Tickets", layout12,location=(t[0],t[1]), size = (600,500), finalize = True)
    t.clear()
    while True:
        event, values = window12.read()
        if event == sg.WIN_CLOSED:
            con.close()
            break
        if event == "Open Tickets":
                u = window12.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window12.close()
                tickets()
                break
        if event == "User List":
                u = window12.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window12.close()
                user_list()
                break
        if event == "View Logged Equipment":
                u = window12.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window12.close()
                out_equipment()
                break
        if event == "Log Equipment Out":
                u = window12.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window12.close()
                new_entry()
                break
        if event == "Select":
                u = window12.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                ti.clear()
                ti = values["J"]
                if len(ti) < 1:
                    sg.popup("Invalid Selection","Please select a ticket",location = (t[0],t[1]))
                    window12.close()
                    tickets()
                window12.close()
                selection()
                break
        if event == "Open Requests":
                u = window12.CurrentLocation()
                t.append(u[0])
                t.append(u[1])
                window12.close()
                xy = "NA"
                yz = "NA"
                zz = "NA"
                requestlist()
                break     
def selection():
    global ti
    layout13 = [[sg.T("")],[sg.Button("View Logged Equipment"),sg.Button("User List"),sg.Button("Log Equipment Out"),sg.B("Open Tickets"),sg.B("Open Requests")],[sg.T("")],[sg.T("ID:      "),sg.T(ti[0][0])],[sg.T("Topic: "),sg.T(ti[0][1])],[sg.T("Issue: "),sg.T(ti[0][2])],[sg.T("Name: "),sg.T(ti[0][4])],[sg.B("Clear")]]
    window13 = sg.Window("IT Admin portal - View Ticket", layout13,location=(t[0],t[1]), size = (600,500), finalize = True)
    event, values = window13.read()
    t.clear()
    while True:
        if event == sg.WIN_CLOSED:
            con.close()
            break
        if event == "Open Tickets":
            u = window13.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window13.close()
            tickets()
            break
        if event == "User List":
            u = window13.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window13.close()
            user_list()
            break
        if event == "View Logged Equipment":
            u = window13.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window13.close()
            out_equipment()
            break
        if event == "Log Equipment Out":
            u = window13.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window13.close()
            new_entry()
            break
        if event == "Clear":
            u = window13.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window13.close()
            cur.execute('update tickets set open_close = true where id = '+str(ti[0][0])+';')
            con.commit()
            ti.clear()
            tickets()
            break
        if event == "Open Requests":
            u = window13.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window13.close()
            xy = "NA"
            yz = "NA"
            zx = "NA"
            requestlist()
            break
def requestlist():
    req = []
    requ = []
    global xy
    global yz
    global zx
    for row in cur.execute('select * from equip_request;'):
        req.append(list(row))
    if len(req) > 0:
        for row in range(len(req)):
            requ.append(str(req[row][1]))
    layout14 = [[sg.T("")],[sg.Button("View Logged Equipment"),sg.Button("User List"),sg.Button("Log Equipment Out"),sg.B("Open Tickets"),sg.B("Open Requests")],[sg.T("")],[sg.T("")],[sg.T("Requests for equipment:")],[sg.Listbox(values = (requ),size = (15,10),key = "K"),sg.B("Details")],[sg.T("Details: ")],[sg.T("Name: "+str(xy))],[sg.T("Equipment Requested: "+str(yz))],[sg.T("Reason: "),sg.Multiline(default_text=str(zx))]]
    window14 = sg.Window("IT Admin Portal - View Open Requests", layout14,location=(t[0],t[1]), size = (600,500), finalize = True)
    event, values = window14.read()
    t.clear()
    xy = "NA"
    yz = "NA"
    zx = "NA"
    while True:
        if event == sg.WIN_CLOSED:
            break
        if event == "Details":
            q = str(values["K"])
            for f in range(len(req)):
                if q[2:-2] in req[f]:
                    xy = str(req[f][1])
                    yz = str(req[f][2])
                    zx = str(req[f][3])
            u = window14.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window14.close()
            requestlist()
            break
        if event == "Open Tickets":
            u = window14.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window14.close()
            tickets()
            break
        if event == "User List":
            u = window14.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window14.close()
            user_list()
            break
        if event == "View Logged Equipment":
            u = window14.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window14.close()
            out_equipment()
            break
        if event == "Log Equipment Out":
            u = window14.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            window14.close()
            new_entry()
            break
        if event == "Open Requests":
            u = window14.CurrentLocation()
            t.append(u[0])
            t.append(u[1])
            xy = "NA"
            yz = "NA"
            zx = "NA"
            window14.close()
            requestlist()
            break
#########################################################################END OF ADMIN SECTION#########################################################################

startup()
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply
#4
Is it really that time wasting to name your variables sensibly? I'd argue not and that you don't really save much time by naming them poorly.

Also, if you need to pass data between functions, just do that (i.e. with parameters); there's no need to use classes.
buran likes this post
Reply
#5
(Sep-23-2021, 09:08 AM)jamesaarr Wrote: With regards to the code, I know it's pooooooorly written, I normally do a messy version that works and then rework it once that's done. The lack of comments is understandable, I never expect anyone else to read the code so I rarely do them. Not an excuse, just a fact.

The mix of GUI and logic is on purpose.

Single letter variables are again for time saving.

The globals are unfortunately needed, as the program needs to pass data between functions and I'm not savvy enough with OOP to be able to do it that way.

Comments and doc-strings are extremely helpful and should not be neglected (i.e. write and update accordingly) even if you are the only one that will ever read your code. Every experienced programmer will tell you that couple of months from now you will desperately need them. We all have been there and this is a lesson everyone learns one way or another. Lack of comments and docs makes it difficult to debug, maintain and extend your program.

Same apply for names - using descriptive names helps a lot in long term, time-savings are short-term benefit and long-term problem.

I don't understand what purpose could be in mixing of logic and GUI. Again it makes difficult to debug, maintain and extend the program.

If you don't wont (or cant) go OOP, still your functions can take arguments and return values. There is rarely need of globals. Globals make it difficult to follow execution flow, debug, maintain and extend your code.

I will repeat myself about repetitive code - again it makes it difficult to maintain.

And by the way p = ("") is same as p = "". In your code p is string, not tuple, if that is your intention.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Lack of comments isn't such a bad thing anyway - you can largely do away with them by writing clear, expressive code and tests that specify the behaviour. I disagree that comments help you maintain code. Maintenance usually requires changing code and comments can't tell you if you've broken anything. Tests, however can, because they'll fail in that case. Tests also serve as documentation, precisely because they specify the behaviour of the system (or parts of it).
I do, however, agree, that mixing GUI code and "business logic" is undesirable. Code that has several concerns (or concepts, responsibilities, etc.) mixed together is inherently more complex to understand.

When we're taught to program, usually the emphasis is on learning the tools in the language to solve problems. This is for good reason, I think, because that's a big enough chunk of stuff for us to understand. Writing maintainable, understandable software goes beyond that though. The book Clean Code by Robert Martin gives a lot of advice about the sorts of things one should think about.
What I'll also say is that nothing is perfect. My team had an issue recently where a library upgrade broke something in one of our applications.

Tests didn't catch it. We had a good test there, which made sense at the time it was written - you can't plan for things you don't know are going to happen and anyway, trying to account for all possible things that could go wrong isn't going to be a good use of time (aside from the things one could reasonably expect to go wrong, of course). Anyway, once we knew what the problem was, we could tweak the test and the failure was reproduced. We made a fix and the test passed, giving us feedback that things were working as expected.

There are different kinds of tests and the app in question is a web app. What we should have also had were smoke tests that hit the running app and check things are OK at a high level (e.g. you get a 200 OK response). These are run at deployment time and if they fail, the deployment doesn't succeed, leaving the previously deployed version.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020