-1

I was making in GUI for a vending machine where i was trying to add images into the button but while making the program there the following error

    Traceback (most recent call last):
      File "e:\kanu\Coding Languages\VS code\4th semester\Data simulation lab\project 
    p2\tempCodeRunnerFile.py", line 8, in <module>
        img_chips = PhotoImage(file= "chps.jpg")
      File "C:\Users\kanu\AppData\Local\Programs\Python\Python310\lib\tkinter\__init_``_.py", 
    line 4093, in __init__
        Image.__init__(self, 'photo', name, cnf, master, **kw)
      File "C:\Users\kanu\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", 
    line 4038, in __init__
        self.tk.call(('image', 'create', imgtype, name,) + options)
    _tkinter.TclError: couldn't open "chps.jpg": no such file or directory

NOTE i have used every format i.e.

file = r'chps.jpg'
file = "chps.jpg"
file = r"chps.jpg"

This is the code that is showing the error

    from tkinter import *
    
    main = Tk()
    #Now we need to make a GUI for the vending machine
    
    
    chips_lb = Label(main, text="Chips")
    img_chips = PhotoImage(file= "chps.jpg")
    chips = Button(main, text ="Chips", image = img_chips).pack()
    
    
    
    mdangl_lb = Label(main, text="Mad Angles")
    img_mdangl = PhotoImage(file = "mdangls.jpg")
    mdangl = Button(main, text ="Mad Angles", image = img_mdangl).pack()
    
    
    
    chklet_lb = Label(main, text="Chocklet")
    img_chklet = PhotoImage(file = "chcklet.jpg")
    chklet = Button(main, text ="Chocklet", image = img_chklet).pack()
    
    
    enter code here
    coke_lb = Label(main, text="Coca Cola")
    img_coke = PhotoImage(file = "coke.jpg")
    coke = Button(main, text ="Coca Cola", image = img_coke).pack()
    
    
    
    bskt_lb = Label(main, text="Biscuit")
    img_bskt = PhotoImage(file = "buiscuit.jpg")
    bskt = Button(main, text ="Biscuit", image = img_bskt).pack()
    
    
    
    pepsi_lb = Label(main, text="Pepsi")
    img_pepsi = PhotoImage(file = "pepsi.png")
    pepsi = Button(main, text ="Pepsi", image = img_pepsi).pack()
    
    
    
    mainloop()

all the file name in this program are correct image as proof for the file name confirmation

1 Answers1

-1

To fix this error make sure that your file path directory is located in the location where your program was created or you may specify the file path directory.

pathlib import Path
print(Path('C:', '/', 'Users'))