Forums

Displaying images in Tkinter with class.

Good morning, I am currently trying to do a 'Vending machine' program in python. I am using Tkinter for the GUI and I have some troubles with it. When displaying an image everything work correctly. Then i create a class 'Product' to simplify the implementation of the multiples product which have the same attribute .I set an Image as an attribute and when displaying with tkinter the 'frame of the picture appear but it is empty and the terminal have no errors. Can you help me please?

#Vending Machine
from tkinter import *
import tkinter.ttk as ttk
from ttkthemes import ThemedStyle

root = Tk()
root.geometry('1000x800')
style = ThemedStyle(root)
style.set_theme("arc")

frame = ttk.Frame(root, width=1000, height=800)
frame.pack()

global myvar
global Unit
myvar = IntVar()
Unit = 0

def add():
    global Unit
    Unit += 1
    myvar.set(Unit)
def substract():
    global Unit
    if Unit == 0 :
        pass
    else:
        Unit -= 1
        myvar.set(Unit)

class Product:
    def __init__(self,ID,name,image,quantity_o,price,Dr,Dc,Dpr,Dpc,P_Btn_r,P_Btn_c,M_Btn_r,M_Btn_c):
        self.ID = ID
        self.name = name
        self.image = image
        self.quantity_o = quantity_o
        self.price = price
        self.Dr = Dr
        self.Dc = Dc
        self.Dpr = Dpr
        self.Dpc = Dpc
        self.P_Btn_r = P_Btn_r
        self.P_Btn_c = P_Btn_c
        self.M_Btn_r = M_Btn_r
        self.M_Btn_c = M_Btn_c

    def Display(self,image,price,Dr,Dc,Dpr,Dpc,P_Btn_r,P_Btn_c,M_Btn_r,M_Btn_c):
        PBtn_image = PhotoImage(file='Plusbutton.png')
        MBtn_image = PhotoImage(file='minusbutton.png')

        DImage = PhotoImage(file=self.image)
        Display_image = Label(frame,image=DImage)
        Display_image.grid(row=Dr, column=Dc)

        Price = ttk.Label(frame,text=price, font=15)
        Price.grid(row=Dpr, column=Dpc)

        P_Btn = Button(frame,image=PBtn_image,command=add, bd = 0)
        P_Btn.grid(row=P_Btn_r, column=P_Btn_c)

        M_Btn = Button(frame, image=MBtn_image, command=substract, bd = 0)
        M_Btn.grid(row=M_Btn_r, column=M_Btn_c)

P001 = Product('P001','Almond Bar','AlmondBar.png',0,'RS35',0,1,3,1,1,2,1,0)
P002 = Product('P002','Orange Juice','juice.png',1,'RS45',0,5,3,5)
P003 = Product('P003','Fires','juice.png',1,'RS45',0,9,3,9)

Product.Display(P001, P001.image, P001.price, P001.Dr, P001.Dc, P001.Dpr, P001.Dpc, P001.P_Btn_r, P001.P_Btn_c, P001.M_Btn_r, P001.M_Btn_c)
Product.Display(P002, P002.image, P002.price, P002.Dr, P002.Dc, P002.Dpr, P002.Dpc, P002.P_Btn_r, P002.P_Btn_c, P002.M_Btn_r, P002.M_Btn_c)
Product.Display(P003, P003.image, P003.price, P003.Dr, P003.Dc, P003.Dpr, P003.Dpc, P003.P_Btn_r, P003.P_Btn_c, P003.M_Btn_r, P003.M_Btn_c)

Count_label = ttk.Label(frame, textvariable=myvar,font=10, padding=10)
Count_label.grid(row=1, column=1)

root.mainloop()

Output and expected output

These forums are mainly for assisting PythonAnywhere users with issues on PythonAnywhere, but perhaps there's someone here that can help you.

Oh I see, thank you