Uk4Life
Uk4Life

Reputation: 3

"IOError No such file or directory" for an image file that IS in the current directory

I'm using Python Imaging Library and Tkinter. I am currently trying to display an image as a label, and I'm getting the above exception. My 35 line source:

from PIL import Image, ImageTk
from Tkinter import Tk, Frame, Label

class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)

        self.parent = parent
        self.initUI()

    def initUI(self):
        self.parent.title("Label")

        self.img = Image.open("diesl.jpg")
        diesl = ImageTk.PhotoImage(self.img)
        label = Label(self, image=diesl)

        label.image = diesl
        label.pack()

        self.pack()

    def setGeometry(self):
        w, h = self.img.size
        self.parent.geometry(("%dx%d+300+300") % (w, h))

def main():
    root = Tk()
    ex = Example(root)
    ex.setGeometry()
    root.mainloop()

if __name__ == '__main__':
    main()

Upvotes: 0

Views: 1573

Answers (3)

user8849169
user8849169

Reputation: 1

I am a newbie to Python but my method works for me. Here's the code:

from Tkinter import *
from PIL import Image,ImageTk
import os
import platform
import webbrowser

sys = platform.system()


def setup_window():
    global window
    window = Tk()
    window.geometry("300x150")
    window.title("Computer Utility GUI")


def style_window():
    path = "/home/pi/Desktop/tool icon"
    img = ImageTk.PhotoImage(Image.open(path))
    label = Label(window, image = img)
    label.grid(row=0,column=0,sticky=W)

setup_window()
style_window()

window.mainloop()

How I got it to work:

1) (You need to import os but in my code I'd already done that.) From the resultant shell window when running my code I typed os.getcwd()

This gives you the current working directory.

2) Make sure your file is saved in this Directory; with your Python script.

3)Type the running directory into the parentheses of wherever you need to use it then place a slash at the end and wait.

4) A little Widget should appear using the arrow keys go down and find the image that should be saved in the path. Then when selecting it press enter and that is the path you need to use,

5) Hopefully it should work. I found Python didn't require the ending.

Upvotes: 0

lc2817
lc2817

Reputation: 3742

Actually, you are right in your comment: Notepad++ seems to launch the files from its own directory.

You can find the solution to your problem here: http://damienlearnsperl.blogspot.com/2009/01/launch-your-perl-script-from-notepad.html (check Lee's comment) if you still intend to use Notepad ++

Upvotes: 2

Anti Earth
Anti Earth

Reputation: 4811

I tried your exact code and it worked perfectly.

Me thinks you're in a strange directory
(such as Program Files which requires special permissions)

Why don't you try ending explorer.exe through task manager,
than running your code again?

Upvotes: 0

Related Questions