如何通过单击“添加书籍”按钮在窗口中显示的书籍列表中插入新行?
我使用了append命令,我认为它已添加到列表中,但未在窗口视图中显示
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()
也许我把清单放在错误的地方。我找不到我的问题。有人可以告诉我如何解决此问题。谢谢。
答案 0 :(得分:0)
在功能bookadd()
中,您要将boook()
对象附加到列表,然后将该列表附加到llistbook
,即书籍属性列表的列表。
您只需在llistbook
后面添加条目中的数据,然后更新图书清单:
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)
des[:-1]
将从Text()
小部件中删除最后一个字符,因为它总是以多余的换行符结尾。