简体   繁体   English

程序在 PhotoImage 中显示错误以打开 tkinter 中的图像说没有这样的文件或目录

[英]program is showing error in PhotoImage to open images in tkinter saying no such file or directory

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我正在为一台自动售货机制作 GUI,我试图将图像添加到按钮中,但是在制作程序时出现以下错误

    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 ie注意我已经使用了每种格式,即

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本程序中所有文件名均正确图片作为文件名确认的证明

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'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM