1

I am doing GUI programming using Tkinter on Python. I am using the grid manager to make widgets. I have created several buttons and I want to upload an image on top of them. When I enter this code, it gives me an escape sequence error.

I heard using PIL is not a good idea? Is that true?

cookImage = PhotoImage(file = "image/C:\Users\terimaa\AppData\Local\Programs\Python\Python36-32\cook.gif")
1
  • please show the exact error. Commented Mar 4, 2017 at 23:25

4 Answers 4

0

Windows filenames must be entered as raw strings:

cookImage = PhotoImage(file=r"C:\Users\terimaa\AppData\Local\Programs\Python\Python36-32\cook.gif")

This applies to all of Python, not just PIL.

Sign up to request clarification or add additional context in comments.

2 Comments

must is a rather strong term. You can also use forward slashes, or backwards slashes that are escaped with backslashes.
@BryanOakley Fair enough, but raw strings are the cleanest way. I never recommend forward slashes since they only mostly work.
0

Use:

path = r"a string with the path of the photo" 

Note the r prefix, it means a raw string.

...
img = ImageTk.PhotoImage(Image.open(file=path))
label = tk.Label(root, image = img)
label.something() #pack/grid/place
...

The path can be:

  • Absolute ("C:\Users\terimaa\AppData\Local\Programs\Python\Python36-32\cook.gif")

  • Relative ("\cook.gif", depends on where the Python code is)

Comments

0

If you have an image file that is exactly what you want, just open it with BitmapImage or PhotoImage. Note that Tcl/Tk 8.6, which you should have with 3.6 on Windows, also reads .png files. On Windows, prefix the filename with 'r' or use forward slashes: 'C:/User/...'.

The actual PIL package is no longer maintained and only works on 2.x. That is what a new user should not use. The compatible successor, pillow (installed for instance with python -m pip install pillow) is actively maintained and works with 3.x. The compatibility extends to the import statement: import PIL imports pillow. Pillows allows one to manipulate images and to convert many formats to tk format (the ImageTk class).

Comments

0

this is exact code which is most help for move image

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import os, shutil

class Root(Tk):
    def __init__(self):
        super(Root,self).__init__()
        self.title("thinter Dialog Widget")
        self.minsize(640,400)

        self.labelFrame = ttk.LabelFrame(self,text="Open A File")
        self.labelFrame.grid(column=0,row=1,padx= 20, pady= 20)
        self.btton()

    def btton(self):
        self.button = ttk.Button(self.labelFrame, text="Browse Afile", command=self.fileDailog)
        self.button.grid(column=1,row=1)
    def fileDailog(self):
        self.fileName = filedialog.askopenfilename(initialdir = "/", title="Select A File",filetype=(("jpeg","*.jpg"),("png","*.png")))
        self.label = ttk.Label(self.labelFrame, text="")
        self.label.grid(column =1,row = 2)
        self.label.configure(text = self.fileName)
        os.chdir('e:\\')
        os.system('mkdir BACKUP')
        shutil.move(self.fileName,'e:\\')



if __name__ == '__main__':
    root = Root()
    root.mainloop()

you could not move image to c drive due to Permission denied: this code work successfully on python 3.8, 3,7

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.