-2
LH=Label(LowerHeading, font=('arial',12,'bold'), text ="Update", bd = 10, width = 15, anchor = 'w')
LH.grid(row=1,column=0)
path = 'C:/Users\Ermira1\Documents\Ermira\1. Sixth Form\Year 13\Computer Science/dress.jpg'
photo = PhotoImage(file="path")
LeftLower= Frame(text=photo)

For some reason this doesnt work and im not sure why.

The error message is displayed below:

Traceback (most recent call last):
  File "C:\Users\Ermira1\Documents\Ermira\1. Sixth Form\Year 13\Computer Science\Inventory (new).py", line 163, in <module>
    photo = PhotoImage(file="path")
  File "C:\Python34\lib\tkinter\__init__.py", line 3416, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Python34\lib\tkinter\__init__.py", line 3372, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "path": no such file or directory
>>> 
user9441606
  • 13
  • 2
  • 4
  • 1
    `r'C:/Users\Ermira1\Documents\Ermira\1. Sixth Form\Year 13\Computer Science/dress.jpg'` - third question I see today, trying to use tkinter and `.jpg` - why not with PIL? PhotoImage is not suited to work with jpg as is :http://effbot.org/tkinterbook/photoimage.htm – Patrick Artner Mar 04 '18 at 13:06
  • 1
    Possible duplicate of [How do I insert a JPEG image into a python Tkinter window?](https://stackoverflow.com/questions/23901168/how-do-i-insert-a-jpeg-image-into-a-python-tkinter-window) – Patrick Artner Mar 04 '18 at 13:09
  • read here: https://stackoverflow.com/a/49094370/7505395 – Patrick Artner Mar 04 '18 at 13:10
  • How do i download the PIL module? – user9441606 Mar 04 '18 at 13:18
  • I dont mean to offend you but can you please show me how to edit my code to make sure it works @PatrickArtner – user9441606 Mar 04 '18 at 13:21
  • 1
    Look closely at the error message. It's telling you there's no such file named `"path"`. Pay attention to the last part -- it's looking for a file with the literal name `"path"` (_not_ with the name _stored_ in the variasble `path`). Though, fix that and you have two more problems: the backslashes don't do what you think they do, and `PhotoImage` doesn't support .jpg. – Bryan Oakley Mar 04 '18 at 14:08
  • Then what do you recommend me to do? – user9441606 Mar 04 '18 at 14:18

1 Answers1

2

The immediate error is that you're trying to get an image from a place called "path", which is by no means a path, you probably meant path.

photo = PhotoImage(file=path)

Which would then produce another error as the string that is path is erroneous as well, it uses \ and / inconsistently; and since \ is an escape character to modify the string in certain ways you should either be replacing them with double (\\) to escape the escape character itself, or use raw string format r"my string such as path". So replace with either:

path = r'C:\Users\Ermira1\Documents\Ermira\1. Sixth Form\Year 13\Computer Science\dress.jpg'

or:

path = 'C:\\Users\\Ermira1\\Documents\\Ermira\\1. Sixth Form\\Year 13\\Computer Science\\dress.jpg'

"How to insert an image without using the PIL module?"

First of all, you can't do that without using an additional library(be it or something else) unless the format is supported by (.jpg, for example, isn't supported by the library).

Secondly, an image can be inserted in a number of ways, for the sake of clarity let's use a label to display.

The code below first downloads images(can be replaced entirely if the images already exist), then it tries to display a .png (it is supported in the versions of later than 8.6), if it can't display .png then it tries to display .gif:

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
except ImportError:
    import Tkinter as tk


def download_images():
    # In order to fetch the image online
    try:
        import urllib.request as url
    except ImportError:
        import urllib as url
    url.urlretrieve("https://i.stack.imgur.com/IgD2r.png", "lenna.png")
    url.urlretrieve("https://i.stack.imgur.com/sML82.gif", "lenna.gif")


if __name__ == '__main__':
    download_images()
    root = tk.Tk()
    widget = tk.Label(root, compound='top')
    widget.lenna_image_png = tk.PhotoImage(file="lenna.png")
    widget.lenna_image_gif = tk.PhotoImage(file="lenna.gif")
    try:
        widget['text'] = "Lenna.png"
        widget['image'] = widget.lenna_image_png
    except:
        widget['text'] = "Lenna.gif"
        widget['image'] = widget.lenna_image_gif
    widget.pack()
    root.mainloop()
Nae
  • 14,209
  • 7
  • 52
  • 79