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
pil or something else) unless the format is supported by tkinter(.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 tk 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()