0

How can I display an icon/image with resize for "Available" or "Out of Stock" displaying it as an image rather than as a text. I would like to display as an image instead. Can I do that? I am using mysql to display all of my queries in the page. Hope you can advise on this as I am new in tKinter Treeview. I tried to search around but I cant fine anything. Thank you.

class Inventory:
def __init__(self, top=None):
    top.geometry("1366x768")
    top.resizable(0, 0)
    top.title("Inventory")

    self.userReg_Form = Label(inv)
    self.userReg_Form.place(relx=0, rely=0, width=1366, height=768)
    self.userReg_FormImage = Image.open("./images/Inventory_availability.png")
    self.userReg_FormImageResized = self.userReg_FormImage.resize((1366, 768), Image.ANTIALIAS)
    self.userReg_FormImage = ImageTk.PhotoImage(self.userReg_FormImageResized)
    self.userReg_Form.configure(image=self.userReg_FormImage)

    self.scrollbarx = Scrollbar(inv, orient=HORIZONTAL)
    self.scrollbary = Scrollbar(inv, orient=VERTICAL)
    self.tree = ttk.Treeview(inv)
    self.tree.place(relx=0.2, rely=0.2, width=880, height=550)
    self.tree.configure(
        yscrollcommand=self.scrollbary.set, xscrollcommand=self.scrollbarx.set
    )
    self.tree.configure(selectmode="extended")

    self.tree.bind("<<TreeviewSelect>>", self.on_tree_select)

    self.scrollbary.configure(command=self.tree.yview)
    self.scrollbarx.configure(command=self.tree.xview)

    self.scrollbary.place(relx=0.954, rely=0.203, width=22, height=548)
    self.scrollbarx.place(relx=0.307, rely=0.924, width=884, height=22)

    self.tree.configure(
        columns=(
            "Status",
            "Product ID",
            "Product_Name",
            "Quantity",
            "Supplier",
            "Price",
            "Purchase Date",
            "Expiration Date",
        ), show = 'tree'
    )
    '''
    self.tree.heading("Status", text="Status", anchor=W)
    self.tree.heading("Product ID", text="Product ID", anchor=W)
    self.tree.heading("Product_Name", text="Product_Name", anchor=W)
    self.tree.heading("Quantity", text="Quantity", anchor=W)
    self.tree.heading("Supplier", text="Supplier", anchor=W)
    self.tree.heading("Price", text="Price", anchor=W)
    self.tree.heading("Purchase Date", text="Purchase Date", anchor=W)
    self.tree.heading("Expiration Date", text="Expiration Date", anchor=W)
    '''
    self.tree.column("#0", stretch=NO, minwidth=0, width=0)
    self.tree.column("#1", stretch=NO, minwidth=0, width=80)
    self.tree.column("#2", stretch=NO, minwidth=0, width=260)
    self.tree.column("#3", stretch=NO, minwidth=0, width=100)
    self.tree.column("#4", stretch=NO, minwidth=0, width=120)
    self.tree.column("#5", stretch=NO, minwidth=0, width=80)
    self.tree.column("#6", stretch=NO, minwidth=0, width=80)
    self.tree.column("#7", stretch=NO, minwidth=0, width=80)
    self.tree.column("#8", stretch=NO, minwidth=0, width=100)
    self.DisplayData()

def DisplayData(self):
    cur.execute("SELECT * FROM inventory")
    fetch = cur.fetchall()


    for data in fetch:
        self.tree.insert("", "end", values=("available" if int(data[2]) > 0 else "out of stock", *data))


sel = []
def on_tree_select(self, Event):
    self.sel.clear()
    for i in self.tree.selection():
        if i not in self.sel:
            self.sel.append(i)
Shadow
  • 33,525
  • 10
  • 51
  • 64
Gayle
  • 17
  • 4
  • 1
    Does this answer your question? [Python Tkinter Treeview add an image as a column value](https://stackoverflow.com/questions/49307497/python-tkinter-treeview-add-an-image-as-a-column-value) – Henry Yik Jul 04 '21 at 09:40
  • Hi @HenryYik, How can I add it since I have SQL code? I have this code "cur.execute("SELECT * FROM inventory") fetch = cur.fetchall() for data in fetch: self.tree.insert("", "end", values=("available" if int(data[2]) > 0 else "out of stock", *data))" – Gayle Jul 04 '21 at 10:40

0 Answers0