'Getting user to select and image and display that said image in Tkinter

I'm currently working on a basic Tkinter application, but I can't seem to get image display working. My class is below

class App:

    def __init__(self):

        self.root = tk.Tk()
        self.root.title("Diabetic Retinopathy Detection")
        self.image = ""

        window_width = 700
        window_height = 550
        # get the screen dimension
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()

        # find the center point
        center_x = int(screen_width/2 - window_width / 2)
        center_y = int(screen_height/2 - window_height / 2)

        # set the position of the window to the center of the screen
        self.root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')

        file_upload = ttk.Button(self.root, text="Upload 2D Fundus Image",
                                 command=lambda: self.upload_file())
        file_upload.pack()

        self.root.resizable(False, False)

        # when loading a file in, root.lower()

    def run(self):
        self.root.mainloop()

    def upload_file(self):
        file_path = askopenfile(mode='r', filetypes=[('Image Files', '*jpeg')])
        if file_path is not None:
            pass
        self.image = file_path.name
        img = ImageTk.PhotoImage(Image.open(file_path.name))

        output = tk.Label(self.root, text=file_path.name, image=img)
        output.pack()


new_app = App()
new_app.run()

The 'output' label displays just the text when I don't set image to anything, I can't figure out why the image doesn't display. I know the image exists because Image.open(file_path.name).show() works. Can anyone figure this out?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source