Home > Back-end >  How to add a image into PyCharm on Mac
How to add a image into PyCharm on Mac

Time:09-27

I am trying to use tkinter to make a GUI and everything works for me except getting an image to import. The error is this


  • Error Message

self.tk.call(('image', 'create', imgtype, name,) options) _tkinter.TclError: couldn't open "MainDisk/Users/Matt/PythonProjects/Car_image.jpg": no such file or directory

  • My line of code

photo = tk.PhotoImage(file="MainDisk/Users/Matt/PythonProjects/Car_image.jpg")


Is this the way that you would import an image on Mac? Sorry I am very confused and just am not sure how to get this to work. The image is in the right place but I have to be doing something wrong clearly. Me and someone else had this working on a windows computer but can't get it to work on Mac. Help is appreciated.


CodePudding user response:

You can try :

photo = tk.PhotoImage(r"MainDisk/Users/Matt/PythonProjects/Car_image.jpg")

The Reason for your error was that your image path contained backslash \ in it. As we know backslashes \ are treated as escape characters by the compiler, and therefore interprets any character following a \ as a escape sequence, which is not our intention here. So, In order to get through this problem provide raw path of your image file, by using r before the image path string.

  • Related