0

Let's say I have this Panel:

panel = ttk.Label(window,image = image1)

I want to know what image he is storing inside of him. Is there any way to get this?

acw1668
  • 40,144
  • 5
  • 22
  • 34
Usual_Coder
  • 99
  • 1
  • 2
  • 6
  • if You are using `PhotoImage` then You can try: `print(image1['file'])` and that will print file path – Matiiss Apr 12 '21 at 23:14

1 Answers1

0

The simplest solution is to attach the image object to the label widget.

panel = ttk.Label(window, image=image1)
panel.image = image1

This solves two problems. For one, it preserves the reference to the image so that it doesn't get destroyed by python's garbage collector. For another, you can then reference the image object itself as long as you have a reference to the image. If you have a reference to the image, you can get the filename with the cget method:

print(f"filename: {panel.image.cget('file')}")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    Please note that it works only for `tkinter.PhotoImage`, but not for `PIL.ImageTk.PhotoImage`. Also `panel.image = image` should be `panel.image = image1` instead. – acw1668 Apr 13 '21 at 02:34