Search code examples
python-3.xlisttkinterrow

How can I insert new rows in tkinter


How can i insert new row in list of book which presents in window by clicking on Add book button?

I have used append command and I think it is added to list but it doesn't be displayed in window view



from tkinter import *
from tkinter import messagebox
from tkinter.ttk import *
from tkinter import ttk


class boook:
    __bookname = ""
    __pricebook = ""
    __likebook = 0
    __writerbook = ""
    __desbook = ""


    def setbook(self,book):
        self.__bookname=book

    def setprice(self,pprice):
        self.__pricebook=pprice

    def  setlike(self,likkk):
        self.__likebook=likkk

    def setwriter(self,writerr):
        self.__writerbook=writerr

    def setdesc(self,descrip):
        self.__desbook=descrip

    def checkname(self,book):
        if self.__bookname==book:
            return True
        return False


llistbook = [["little girl", "$29.99", 100,"Leo SS","it's nice book "],
             ["Blue Sky", "$49.99", 50, "O. Lee ", "You may have read this beloved coming-of-age story when you were in school."],
             ["White", "$19.99", 73, "JJ", "Boring Book"]]
row=[]

def bookadd(llistbook,name,pr,lik,writ,des):
    bb=boook()
    bb.setbook(name)
    bb.setprice(pr)
    bb.setlike(lik)
    bb.setwriter(writ)
    bb.setdesc(des)
    row.append(bb)
    llistbook.append(row)






def addbook():
    bookadd(llistbook,bookname.get(),price.get(),likk.get(),writer.get(),desc.get(1.0,END))
    bookname.delete('0', 'end')
    price.delete('0', 'end')
    likk.delete('0', 'end')
    writer.delete('0', 'end')
    desc.delete(1.0,END)

def deletebook():
        c = tree1.selection()
        tree1.delete(c)


def editbook():
    c = tree1.selection()
    values1 = tuple(tree1.item(c)['values'])
    bookname.insert('end', values1[0])
    price.insert('end', values1[1])
    likk.insert('end', values1[2])
    writer.insert('end', values1[3])
    desc.insert('end', values1[4])

def showinfoadmin():
    def back():
        window01.destroy()


    window01 = Tk()
    c = tree1.selection()
    valuesadmin = tuple(tree1.item(c)['values'])
    T = Text(window01, height=20, width=45)
    T.pack()
    T.insert(END, "{0}\nPrice: {1}\nLike:{2}\nwriter:{3}\ndescription:{4}\n".format(valuesadmin[0],
                                                                                    valuesadmin[1],
                                                                                    valuesadmin[2],
                                                                                    valuesadmin[3],
                                                                                    valuesadmin[4]))

    backb = Button(window01, text="Back", command=back)
    backb.pack()


    window01.mainloop()

window1 = Tk()
window1.geometry('800x600')
window1.title("Admin")
tree1 = Treeview(window1, show="headings")
tree1["columns"] = ("one", "two", "three")
tree1.column("one", width=150)
tree1.column("two", width=90)
tree1.column("three", width=40)
tree1.heading("one", text="Book Name")
tree1.heading("two", text="Price")
tree1.heading("three", text="Like")

for item in llistbook:
    tree1.insert('', 'end', values=item)

tree1.pack(padx=5, pady=10, side=LEFT)
B1admin = Button(window1, text="Information", command=showinfoadmin)
B1admin.place(x = 00,y = 10)

bottdel = Button(window1,text = "Delete Book",command =deletebook)
bottdel .place(x = 00,y = 40)

bottedit = Button(window1,text = "Edit Book",command =editbook)
bottedit .place(x = 00,y =70)

lbllike =Label(window1, text="Like")
lbllike.place(x = 440,y = 80)
likk = Entry(window1, width=20)
likk.place(x = 440,y = 100)

lblbook =Label(window1, text="Book Name")
lblbook.place(x = 440,y = 130)
bookname = Entry(window1, width=20)
bookname.place(x = 440,y = 150)

lblprice = Label(window1, text="price")
lblprice.place(x = 440,y = 180)
price = Entry(window1, width=20)
price.place(x = 440,y = 200)

lblwriter =Label(window1, text="writer")
lblwriter.place(x = 440,y = 240)
writer = Entry(window1, width=20)
writer.place(x = 440,y = 260)

lblinf =Label(window1, text="Description")
lblinf.place(x = 440,y = 280)

desc =Text(window1, height=10, width=30)
desc.place(x = 440,y = 300)

bottadd = Button(window1,text = "Add Book",command =addbook)
bottadd .place(x = 440,y = 470)

window1.mainloop()

Maybe I put the listbook in the wrong place. I can't find my problem. Can anybody tell me how can i fix this problem.Thanks in advance.


Solution

  • in the function bookadd() you are appending a boook() object to a list and then appending that list to llistbook, which is a list of lists of book attributes.

    You could just append the llistbook with data from the entrys and then update the book listing:

    def bookadd(llistbook, name, pr, lik, writ, des):
        bb = [name, "${}".format(pr), lik, writ, des[:-1]]
        llistbook.append(bb)   # Appends new book to llistbook
        # Update the list of books:
        tree1.insert('', 'end', values=bb)
    

    The des[:-1] removes the last character from the Text() widget as it's always ended by an extra newline.