-1

I am attempting to display an image from a file in my program. The python file is in the same folder as my image file and I have the file name correct. When it displays on the screen, the space is taken up where the image should be, but no image shows.

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

cardImageLabel=Label(playerFrame, image=tk.PhotoImage(file="2C.png")).grid(row=1, column=0)

This is my code, it's a bit ugly and I should separate it but I'm more used to formatting it like this.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
James Mann
  • 27
  • 3
  • For the same reason they don't show up as discussed in [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) — and has the same solution. – martineau Mar 27 '21 at 15:06
  • Your example is incomplete. Is the cod that uses the image inside a function? – Bryan Oakley Mar 28 '21 at 04:09

1 Answers1

1

You need to create a reference to the image, to prevent collecting it by garbage collector, as there is no reference created to this object by PhotoImage class itself.

img = tk.PhotoImage(file="2C.png")
Witekkq
  • 11
  • 1
  • 1