Maxbo
Maxbo

Reputation: 23

Tkinter "couldn't open "pyimage1": no such file or directory" error when trying to open an image

I have been trying to add a background image to my Interface but I always get the error:

"couldn't open "pyimage1": no such file or directory"

Also I am pretty new to python.

Already tried multiple methods with tkinter and PIL aswell as tkinter's canvas which didn't work either

This is the whole programm:

 import tkinter as tk
 from PIL import Image, ImageTk

class MainMenu:

def __init__(self, master):

    #creating Main Frame and Window
    master.title("Interface")

    #Image

    image = Image.open(r"Images\matrix.jpg")
    photo = ImageTk.PhotoImage(image)
    self.background_image = tk.PhotoImage(file=photo)
    self.background_label = tk.Label(image=self.background_image)
    self.background_label.place(x=0,y=0)
    self.background_label.img = self.background_image

    #Creating Widgets
    self.label1 = tk.Label(master, text="Please Enter the text you would
    like encrypted: ")
    self.entry1 = tk.Text(master, height=5, width=20)
    self.button = tk.Button(master, text="Submit", command=self.Submit)

    #Adding Widgets to Grid
    self.label1.grid(row=0, column=0, padx=5, pady=5)
    self.entry1.grid(row=1, column=0, padx=5, pady=5)
    self.button.grid(columnspan=2, pady=10)

    #Configuration of Widgets and Main window
    master.configure(bg="black")
    self.button.configure(bg="light blue")
    self.label1.configure(bg="black", fg="light blue")                 
    self.entry1.configure(bg="light blue")    


def Submit(self):
    print("You entered: " + self.entry1.get())


root = tk.Tk()
Mm = MainMenu(root)
root.mainloop()

The main problem would be within these lines I am guessing:

    image = Image.open(r"Images\matrix.jpg")
    photo = ImageTk.PhotoImage(image)
    self.background_image = tk.PhotoImage(file=photo)
    self.background_label = tk.Label(image=self.background_image)
    self.background_label.place(x=0,y=0)
    self.background_label.img = self.background_image

As you can see I am trying to make an Interface or GUI and everything is working fine except the background image.

Upvotes: 2

Views: 5046

Answers (2)

Travis Wilson
Travis Wilson

Reputation: 1

As far as I can tell this simply means you have used 'tk.PhotoImage' twice on a variable.

For example:

item1_image = tk.Label()
image = tk.PhotoImage('image.png')
item1_image.configure(image=tk.PhotoImage(image))

When you're pulling these variables out of different places in a large file, it is hard to keep track of whether 'PhotoImage' is used. I typically use it as early as possible to avoid the image not appearing.

Upvotes: 0

RoyM
RoyM

Reputation: 747

Try this:

image = Image.open("Images\matrix.jpg")
photo = ImageTk.PhotoImage(image)
#self.background_image = tk.PhotoImage(file=photo) -- Not needed, delete
self.background_label = tk.Label(image=photo)
self.background_label.image = photo
self.background_label.place(x=0,y=0)
#self.background_label.img = self.background_image -- Also not needed, delete

Upvotes: 1

Related Questions