Python Forum
Get the parent label of a ImageTk.PhotoImage
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get the parent label of a ImageTk.PhotoImage
#1
Hi,
i have five labels with a Photo in a frame:
            for entry in dir:
                file = self.file_path + "\\" + entry
                if os.path.isfile(file):
                    try:
                        label = tk.Label(self.inner, text=location_date_str + entry)
                        label.pack(side="top", anchor=tk.NW, pady=(10, 0))
                        image = ImageTk.PhotoImage(Image.open(file))
                        image_label = tk.Label(self.inner, image=image)
                        self.photos.insert(0, image) # to prevent garbage collector from remove...
                        image_label.pack(side="top", anchor=tk.NW)
                    except:
                        tk.messagebox.showinfo(title="Exception", message=traceback.format_exc())
Now to zoom in and out i guess that i can't just change the image, because the new image would not have the same reference as the old one and therefore the label wouldn't get the new but hold the old one.
So i tried to get the parent label of the image to set the new image onto like this:

    def zoom_in(self, event=None):
        print("zoom in")
        for image in self.photos:
            # get the parent (label) of the image
            label = image.winfo_parent()
            # get a zoomed image
            image = image._PhotoImage__photo.zoom(2)
            label.config(image=image)
Error message: 'PhotoImage' object has no attribute 'winfo_parent'

So:
- Is this function not impelemented, why?
- how can i get the parent of the image?
- is there another way to zoom such images?
Reply
#2
The things in tkinter that have parents are all windows (I think) and windows have parent windows. An image is a thing you can paint into a window. I can paint the same image into multiple windows. The image is not a window, and thus it does not have a parent window.

You could find what label uses an image. Labels must have some way to reference images, otherwise a label could redraw it's label when the window is refreshed. We also know this cannot be a strong reference or it we wouldn't have to make a strong reference to keep labels around. Labels have functions "image_names()" and "image_types()". When I run this code:
from tkinter import *
from PIL import Image, ImageTk

root = Tk()
image = ImageTk.PhotoImage(Image.open('image.png'))
label = Label(root, text='Hi', image=image)

print(image)
print(label.image_names())
I get this:
Output:
pyimage1 ('pyimage1', '::tk::icons::information', '::tk::icons::error', '::tk::icons::warning', '::tk::icons::question')
I could not find support for using the image name to find windows that use the image, but it could be done. Using winfo_children() to get a list of all child windows you could ask each one for their image_names() and replace images that match the old image.

But that would be silly.

You are already maintaining a list of images to provide a handle to save them from the garbage collector, why not save the "parent" too? Instead of using a list, us a dictionary to save the image and use the label as the key.
for entry in dir:
    file = self.file_path + "\\" + entry
    if os.path.isfile(file):
        try:
            label = tk.Label(self.inner, text=location_date_str + entry)
            label.pack(side="top", anchor=tk.NW, pady=(10, 0))
            image = ImageTk.PhotoImage(Image.open(file))
            image_label = tk.Label(self.inner, image=image)
            self.images[label] = image
            image_label.pack(side="top", anchor=tk.NW)
        except:
            tk.messagebox.showinfo(title="Exception", message=traceback.format_exc())
You could even maintain a list of images for different zooms.
self.images[label] = {'image':image, '2x':image._PhotoImage__photo.zoom(2)}
To zoom in:
label.config(image=self.images[label]['2x']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question on tkinter canvas PhotoImage gr3yali3n 1 2,134 Sep-05-2020, 12:18 PM
Last Post: Larz60+
  Gif with PhotoImage Friend 3 5,285 Jul-23-2019, 10:23 PM
Last Post: Friend
  Move PhotoImage dan789 2 2,702 Dec-19-2018, 06:00 PM
Last Post: dan789
  [Tkinter] PhotoImage and Jpeg images. rozen 3 4,579 Jan-07-2018, 08:34 AM
Last Post: Gribouillis
  Ho can I get the first parent of a class? panoss 2 3,601 Jan-10-2017, 08:10 AM
Last Post: panoss
  [Tkinter] createing a tkinter photoimage from array in python3 pootle 2 16,811 Oct-18-2016, 09:28 AM
Last Post: pootle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020