0

In the next code I am tryng to display an image in my frame:

from tkinter import PhotoImage
import tkinter as tk
from PIL import Image, ImageTk

root = tk.Tk()
root.title("My window")
root.geometry('500x400')

frame = tk.Frame(root)
frame.pack(fill='both', expand=1)

def insertImage():
    picture = ImageTk.PhotoImage(Image.open('pic.jpeg'))
    lbl_picture_img = tk.Label(frame, image=picture)
    lbl_picture_img.pack()

# picture = ImageTk.PhotoImage(Image.open('pic.jpeg'))
# lbl_picture_img = tk.Label(frame, image=picture)
# lbl_picture_img.pack()
insertImage()

root.mainloop()

But when I run insertImage() I just get an empty window. But if I uncomment those 3 lines and run my code, I can see my window with the picture:

Picture with uncomment lines

My goal is to display the picture by running a method, that is why I am not satisfied by displaying it with the comment lines.I hope you can help me, thanks.

bedipat
  • 21
  • 3
  • The image object used by tkinter must be global. So, that it doesn't get garbage collected. Well, it must exist for the scope of the image display. I had the same problem with image buttons. – bitRAKE Oct 04 '22 at 12:46

0 Answers0