首页 > 解决方案 > 使用 Tkinter Treeview 进行数据库处理时无法查看图像

问题描述

我正在开发一个数据库项目,运行以下代码不会显示给定的图像。完整的代码可以在这里找到。

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,方法是在终端中键入“bash init.sh”,然后运行“main.py”。对于测试,输入详细信息:“Nikhil”作为用户名,“hi”作为密码,选择角色为“客户”,然后选择“输入”,然后选择“浏览产品”并输入“asg”。

标签: pythondatabaseimagetkintertreeview

解决方案


固定的!问题出在线路上

plist['show'] = 'headings'

删除后,图像出现。


推荐阅读