1

For some reason when I run my program the image doesn't show. I only get a greyed out box and no errors when running. What I want is when the user clicks the upload button a second window will open and ask for their email address and password. The file I want in that greyed out position is a logo (above the email request). The files cats.jfif is in the same folder as my program. Ultimately I would like to add in a png image (don't know if that matters or not). Code and image of output is below:

import tkinter as tk
from tkinter import ttk
import tkinter.font as tkFont
from PIL import ImageTk
from PIL import Image as PilImage

class Other:
    def __init__(self,master):
        self.width = 300
        self.height = 500
        master.geometry(f"{self.width}x{self.height}")
        self.frame = tk.Frame(master,bg="white")
        self.frame.place(relx=0, rely=0, relwidth=1, relheight=1)
        self.font = ('Helvetica', '10')
        self.uploadButton = tk.Button(self.frame, text="Upload", bg="orange", fg="white", font=self.font,
                                command = self.Upload)
        self.uploadButton.place(relx=0.001, rely=0.865, relwidth=0.999, relheight=0.13)

    def Upload(self):
        #HAVE TO CHECK IF FILE EXISTS IN THE FOLDER
        #f"{self.date}_iProdi.xlsx"
        login = tk.Toplevel()
        login.geometry("300x500")
        self.frame2 = tk.Frame(login, bg = "white")
        self.frame2.place(relx=0, rely=0, relwidth=1, relheight=1)
        img = ImageTk.PhotoImage(PilImage.open("cats.jfif"))
        self.panel = tk.Label(self.frame2, image = img)
        self.panel.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.2)

        #Employee username
        self.emailLabel = tk.Label(self.frame2, text="Email:", font=self.font, bg="white", anchor="e")
        self.emailLabel.place(relx=0.1, rely=0.4, relwidth=0.25, relheight=0.08)
        self.emailEntry = tk.Entry(self.frame2, font=self.font, bg="white")
        self.emailEntry.place(relx=0.37, rely=0.4, relwidth=0.4, relheight=0.06)

        #Employee Password
        self.passwordLabel = tk.Label(self.frame2, text="Password:", font=self.font, bg="white", anchor="e")
        self.passwordLabel.place(relx=0.1, rely=0.56, relwidth=0.25, relheight=0.08)
        self.passwordEntry = tk.Entry(self.frame2, font=self.font, bg="white", show="*")
        self.passwordEntry.place(relx=0.37, rely=0.56, relwidth=0.4, relheight=0.06)



root = tk.Tk()
other = Other(root)
root.mainloop()

Greyed out box above email is supposed to be the image

Manib
  • 171
  • 15
  • Change `img = ImageTk.PhotoImage(...)` to `self.img = ...`. Similar problem has been asked many times. – acw1668 Jan 08 '20 at 10:31

2 Answers2

3

In tkinter there is a problem with Load images. When you try to load an image you have to be sure that the variable in which is stored the loaded image doesn't get destroyed or the image will be destroyed in tkinter too.

In you code you are loading the image in the img local variable, that gets 'destroed' after that the method is executed and so your image gets deleted do.

For save the image reference in this case you need only to add self. befor the variable name to save the variable as a class variable and preventing its tresholding

self.img = ImageTk.PhotoImage(PilImage.open("4.jpg"))
self.panel = tk.Label(self.frame2, image = self.img)

Common usage is to store the images references in an array

here is a similar question

Mat.C
  • 1,379
  • 2
  • 21
  • 43
2

Found the solution here.

Apparently tkinter destroys the image and so you need to keep a reference of it yourself.

Manib
  • 171
  • 15