Reputation: 75
I am told that I can add images to a label, but when I run the following code I get an error message:
unicode error: unicodeescape codec can't decode bytes in position 2-3: truncated \UXXXXXX escape
My code is as simple as possible
from tkinter import *
root = Tk()
x = PhotoImage(file="C:\Users\user\Pictures\bee.gif")
w1 = Label(root, image=x).pack()
root.mainloop()
All the examples I've seen don't include the file path to the image but in that case Python can't find the image.
What am I doing wrong ??
Upvotes: 2
Views: 703
Reputation: 386382
Python is treating \Users
as a unicode character because of the leading \U
. Since it's an invalid unicode character, you get the error.
You can either use forward slashes ("C:/Users/user/Pictures/bee.gif"
), a raw string (r"C:\Users\user\Pictures\bee.gif"
), or escape the backslashes ("C:\\Users\\user\\Pictures\\bee.gif"
)
Upvotes: 2