python – My buttons won’t do what I asked them to do ;(

So I’m trying to make a simple list to keep inventory. I have added buttons to List the current items, delete, edit, and add items. So far I was able to get the list and quit buttons to work. I’m not sure as to why the other 3 don’t work 🙁

Functions seem to be calling the right things but it won’t display.

For example, when I enter the info to the list editor it will modify the inventory.txt file but won’t display it on the screen?


file1 = "inventory.txt"

root = Tk()      
root.title("Buttons")

root.geometry('630x600')
root.config(bg='grey')

Prod = list()
NewWidgetNames = ()

frame1 = Frame(root, bd = 5)   
frame2 = Frame(root, bd = 5)
frame3 = Frame(root, bd = 5)
frame4 = Frame(root, bd = 5)
frame5 = Frame(root, bd = 5)
frame6 = Frame(root, bd = 5, bg = "blue")
frame7 = Entry(root, bd = 5)
frame9 = Entry(root, bd = 5)
frame10 = Entry(root, bd = 5)
frame11 = Entry(root, bd = 5)
frame12 = Entry(root, bd = 5)
frame13 = Entry(root, bd = 5)
frame14 = Entry(root, bd = 5)
frame15 = Entry(root, bd = 5)
frame16 = Entry(root, bd = 5)




frame1.grid(row = 1, column = 1)
frame2.grid(row = 1, column = 2)
frame3.grid(row = 1, column = 3)
frame4.grid(row = 1, column = 4)
frame5.grid(row = 1, column = 5)
frame6.grid(row = 2, column = 1)
frame7.grid(row = 8, column = 1)
frame9.grid(row = 3, column = 1)
frame10.grid(row = 4, column = 1)
frame11.grid(row = 5, column = 1)
frame12.grid(row = 6, column = 1)
frame13.grid(row = 7, column = 1)
frame14.grid(row = 8, column = 1)
frame15.grid(row = 10, column = 1)
frame16.grid(row = 11, column = 1)

label1 = Label(root,text="Product ID",bd=3,bg="red")
label2 = Label(root,text="Vender ID",bd=3,bg="red")
label3 = Label(root,text="Buy Cost",bd=3,bg="red")
label4 = Label(root,text="Quantity",bd=3,bg="red")
label5 = Label(root,text="Sell Cost",bd=3,bg="red")
label6 = Label(root,text="Name",bd=3,bg="red")
labelShow = Label(root,bg="blue")

label1.place(x=140,y=253)
label2.place(x=140,y=280)
label3.place(x=140,y=308)
label4.place(x=140,y=335)
label5.place(x=140,y=361)
label6.place(x=140,y=388)
labelShow.place(x=140,y=500)


class Product: #create a class
    def __init__(self,pd_NM,pd_ID,vd_ID,cst,stc,loc):   # create instances
                             
        self.prod_NM = pd_NM                            
        self.prod_ID = pd_ID    
        self.vend_ID = vd_ID
        self.cost_IN = cst 
        self.stock_LVL = stc
        self.shelf_LOC = loc


listbox = Listbox(frame6)
listbox.pack()

def Quit():
    quit()
    
def AddWid(): #adds a new product

    inName = frame7.get()  #create some outputs to create a new product
    inID = frame9.get()
    inVend = frame10.get()
    inCst = frame11.get()
    inQty = frame12.get()
    inLoc = frame13.get()


    Prod.append(Product(inName,inID,inVend,inCst,inQty,inLoc))  #append new product information to the list and send instances to the class
        
    f1 = open(file1,"a")    #open the file and 'a' - add
    f1.write(inName + "," + inID + "," + inVend + "," + inCst + "," + inQty + "," + inLoc + "n") #write new item to the file and separate with comas
    f1.close()  #close the file
    
    listbox.delete(0, END)
    f1 = open(file1,"r")    #open the file and 'r' - read
    for strln in f1:        #create a for loop which will read each line of the file 
        strln = strln.strip("n")   #and get rid of spaces
        strSep = strln.split(",")   #and then separate with comas
        Prod.append(Product(strSep[0],strSep[1],strSep[2],strSep[3],strSep[4],strSep[5]))   #send instances to the class and put all information into the list 'Prod' 
        listbox.insert(0, strSep[0])
        


def listB4(): #writes some products into the file, reads em and puts into the listbox

    f1 = open(file1,"w")    #open that file and 'w' - write
    Product1 = f1.write("Widget,A11111,Manga,10.50,1000,A1n")  # put some stuff in it 
    Product2 = f1.write("Apple,S21312,Fruit,9.50,5000,F1n")
    Product3 = f1.write("Bobbles,B99,Toe,99,10,Z1n")
    Product4 = f1.write("Thingamabobs,123,Costco,100,8,D6n")
    f1.close()

    listbox.delete(0, END)
    f1 = open(file1,"r")    #open the file and 'r' - read
    for strln in f1:        #create a for loop which will read each line of the file 
        strln = strln.strip("n")   #and get rid of spaces
        strSep = strln.split(",")   #and then separate with comas
        Prod.append(Product(strSep[0],strSep[1],strSep[2],strSep[3],strSep[4],strSep[5]))   #send instances to the class and put all information into the list 'Prod' 
        listbox.insert(0, strSep[0])


def Edit(): #supposedto edit product(works at some point)
    flag1 = False
    flag2 = False
    flag3 = False
    for i in listbox.curselection():
        selectedItem = listbox.get(i)
    
        for item_index in range(len(Prod)):
            # Check an intput name
            
            if selectedItem == Prod[item_index].prod_NM:
                flag1 = True
                editAction = frame15.get()

                if editAction == "name":
                    newName = frame7.get()
                    Prod[item_index].prod_NM = newName
                    flag2 = True

                elif editAction == "id":
                      newID = frame9.get()
                      Prod[item_index].vend_ID = newID
                      flag2 = True


                elif editAction == "vendor":
                      newVendor = frame10.get()
                      Prod[item_index].cost_IN = newVendor
                      flag2 = True

                elif editAction == "cost":
                      newCost = frame11.get()
                      Prod[item_index].cost_IN = newCost
                      flag2 = True


                elif editAction == "sell":
                      newStock = frame12.get()
                      Prod[item_index].sell_PRC = neStock
                      flag2 = True

                elif editAction == "location":
                      newLocation = frame13.get()
                      Prod[item_index].shelf_LOC = newLocation
                      flag2 = True

            if flag1 == False:
                print("Product is not in the list. n")

            elif flag2 == True:
                f1 = open (file1, "w")
                for item_index in range(len(Prod)):
                    f1.write("%s, %s, %s, %s, %s, %s, %sn " %(Prod[item_index].prod_NM, Prod[item_index].prod_ID, Prod[item_index].vend_ID, Prod[item_index].cost_IN, Prod[item_index].stock_LVL, Prod[item_index].sell_PRC, Prod[item_index].shelf_LOC))
                f1.close()
                print("The product has been updated.n")


            elif flag3 == True:
                print("The action was canceled.n")
    
    

butList = Button(frame1, text = "List", height = 1, width = 6,
       activebackground = "black", activeforeground = "red",
       bg = "red", fg = "black", font = "Verdana 14",
       bd = 15, command = listB4)
butDelete = Button(frame2, text = "Delete", height = 1, width = 6,
       activebackground = "black", activeforeground = "red",
       bg = "red", fg = "black", font = "Verdana 14",
       bd = 15)
butEdit = Button(frame3, text = "Edit", height = 1, width = 6,
       activebackground = "black", activeforeground = "red",
       bg = "red", fg = "black", font = "Verdana 14", command = Edit,
       bd = 15)
butAdd = Button(frame4, text = "Add", height = 1, width = 6,
       activebackground = "black", activeforeground = "red",
       bg = "red", fg = "black", font = "Verdana 14", command = AddWid,
       bd = 15)
butQuit = Button(frame5, text = "Quit", height = 1, width = 6,
       activebackground = "black", activeforeground = "red",
       bg = "red", fg = "black", font = "Verdana 14",
       bd = 15, command = Quit)





butList.pack()   
butDelete.pack()   
butEdit.pack()   
butAdd.pack()   
butQuit.pack()   



root.mainloop()

Leave a Comment