I am working on a GUI to drive a robot wireless over a network. I am using pictures instead of label text for the arrows. I am correctly able to display a left arrow png graphic when I use this code:
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
leftImage = ImageTk.PhotoImage(Image.open("C:\Users\usr\Desktop\left.png"))
#rightImage = ImageTk.PhotoImage(Image.open("C:\Users\usr\Desktop\right.png"))
class GUI:
def __init__(self, master):
frame = Frame(master)
frame.grid()
left = Label(root, image = leftImage)
left.grid(row=1, column=0)
#right = Label(root, image = rightImage)
#right.grid(row=1, column=2)
app = GUI(root)
root.mainloop()
Here is where it gets weird to me. When I remove the comments on the right arrow to try and include a left and right arrow, I get an error. The error is:
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\Users\\usr\\Desktop\right.png'
I can't seem to figure out why it suddenly turns the "\" to a "\ \" for the path on the right arrow. Yet, it won't do draw an error on the path for the left arrow. I'm positive both files are on the right place. Any ideas on why the path of the rightImage is being interpreted differently than the left?
You should use raw string literals for file paths on Windows (note
r
before the double quote):It worked differently for the rightImage because
\r
is a special character (yes, it's one character, not two) - carriage return.To quote a great man (and Python documentation):