0

I'm working with Tkinter and developing a program that uses a PNG image, here is my code:

from tkinter import *
from PIL import ImageTk, Image

class Application:
    def __init__(self, master=None):
        self.ProgramaTOP = Frame(master)
        img = ImageTk.PhotoImage(Image.open("company.png"))
        self.ProgramaTOP.pack()
        self.msg = Label(self.ProgramaTOP, text="Teste Senha", foreground="#00338d", image= img)
        self.msg["font"] = ("Univers", "12", "bold")
        self.msg.pack()
        self.TesteSenha = Button(self.ProgramaTOP)
        self.TesteSenha["text"] = "Teste Senha"
        self.TesteSenha["font"] = ("Univers", "12", "bold")
        self.TesteSenha["width"] = 10
        self.TesteSenha["command"] = self.ProgramaTOP.quit
        self.TesteSenha.pack()

root = Tk()
Application(root)
root.mainloop()

When I try to run got this error:

File "C:\Users\lucascruz\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\Image.py", line 2809, in open fp = builtins.open(filename, "rb") FileNotFoundError: [Errno 2] No such file or directory: 'company.png'

I know that is a path problem, how can I do to program open the image from the directory where my program is running?

In the future, this program will distribuited to other people, in other machines so the code need to be flexible for every machine, my idea is to create a folder with the stored images and the code acess this folder.

How can I do this?

Nathan
  • 3,558
  • 1
  • 18
  • 38
Lucas Cruz
  • 19
  • 9
  • Does this answer your question? [Image does not open with python imaging library](https://stackoverflow.com/questions/17778258/image-does-not-open-with-python-imaging-library) – FrainBr33z3 Jan 22 '20 at 15:35
  • Try `"./company.png"`. The `./` should tell python to look in the working directory. – Mike - SMT Jan 22 '20 at 15:45
  • Yeah, this worked @FrainBr33z3 , but the image occupied all the window and my text box disappear, any way to resize image? – Lucas Cruz Jan 22 '20 at 16:04
  • Its doesn't worked @Mike-SMT same error, thanks – Lucas Cruz Jan 22 '20 at 16:05
  • If you are getting the same error then your image file is not in the same working directory as your py file. – Mike - SMT Jan 22 '20 at 16:17
  • @LucasCruz The question is already answered [here](https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio) – FrainBr33z3 Jan 23 '20 at 09:26

0 Answers0