无法使用Tkinter Treeview来查看数据库处理的图片

2024-10-03 17:23:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理一个数据库项目,运行下面的代码不会显示给定的图像。整个代码可以在here中找到

def populateProducts (self, productName, plist):
    rows = self.db.getProductsFromNameNIL(productName)
    auximage = Image.open ("/home/sourabh/Documents/Github/DBMS-Lab-Project/TkinterReference/small.png")
    self.auxphoto = ImageTk.PhotoImage (auximage)
    plist.delete (*plist.get_children ())
    for row in rows:
        print (row)
        plist.insert ('', 'end', text = '#0s text', values = row, image = self.auxphoto)
def browse (self):
    browseWin = Tk ()
    browseWin.title ("Browse Products")
    browseWin.protocol("WM_DELETE_WINDOW", lambda: self.on_closing (browseWin))  # handle window closing
    Label(browseWin, text = "Enter product name").grid (row = 0, column = 0, sticky = W)

    prodText = StringVar()

    Entry(browseWin, textvariable=prodText).grid (row = 0, column = 1, sticky = W)
    Button (browseWin, text = 'Switch to Login', command = lambda: self.switchToLogin (browseWin)).grid (row = 20, sticky = W, pady = 4)
    ############ Product List #############
    plist = ttk.Treeview (browseWin)
    plist['columns'] = ('pid', 'pname', 'sellerid', 'price', 'tstock', 'pickupaddress', 'description', 'rating')
    plist.heading('#0', text = 'Pic directory', anchor = 'center')
    plist.column ('#0', anchor = 'w', width = 200)
    plist.heading ('pid', text = 'Product ID')
    plist.heading ('pname', text = 'Product Name')
    plist.heading ('sellerid', text = 'Seller ID')
    plist.heading ('price', text = 'Price')
    plist.heading ('tstock', text = 'Total Stock')
    plist.heading ('pickupaddress', text = 'Pickup Address')
    plist.heading ('description', text = 'Description')
    plist.heading ('rating', text = 'Rating')
    plist['show'] = 'headings'
    plist.grid(row = 1, column = 0, rowspan = 18, columnspan = 50)
    Button(browseWin, text= 'Search', command= lambda: self.populateProducts (prodText.get (), plist)).grid(row=0, column = 2, sticky=W)
    browseWin.mainloop()

如果要运行要测试的项目,请在该GUI目录中运行init.sh,方法是在terminal中键入'bash init.sh',然后运行'main.py'。对于测试,请输入详细信息:用户名为“Nikhil”,密码为“hi”,选择角色为“Customer”,然后选择“enter”,然后选择“Browse Products”,然后输入“asg”


Tags: 项目lambda代码textselfdefcolumnproduct