Home > Mobile >  Tkinter Image not displaying in label or canvas
Tkinter Image not displaying in label or canvas

Time:10-10

Here is my code actually creating these labels or canvases:

class FrameBanner(tk.Frame):
    """

    """
    def __init__(self, root, color_black, color_yellow, width, height):

        # Initialize from parent
        super(FrameBanner, self).__init__(root)

        # # Left STAR Banner
        # self.banner_STAR = tk.Canvas(master=self, width=width*0.8, height=height, bg=color_yellow)
        self.image_STAR = ImageTk.PhotoImage(Image.open("images_2/logo_here.png").resize((round(width*0.7), round(height*0.9)), Image.NEAREST))
        # self.banner_STAR.create_image(10, 10, anchor="w", image=self.image_STAR)
        # self.banner_STAR.image = self.image_STAR
        self.banner_STAR = tk.Label(master=self, image=self.image_STAR)
        self.banner_STAR.image = self.image_STAR

        # # Right IMS Banner
        # self.banner_IMS = tk.Canvas(master=self, width=width*0.2, height=height, bg=color_black)
        self.image_IMS = ImageTk.PhotoImage(Image.open("images_2/logo_here.png").resize((round(width*0.15), round(height*0.9)), Image.NEAREST))
        # self.banner_IMS.create_image(width*0.2, 10, anchor="w", image=self.image_IMS)
        # self.banner_IMS.image = self.image_IMS
        self.banner_IMS = tk.Label(master=self, image=self.image_IMS)
        self.banner_IMS.image = self.image_IMS

        # Grid Layout
        self.banner_STAR.grid(column=0, row=0, sticky='w')
        self.banner_IMS.grid(column=1, row=0, sticky='e')

Here is my code where it is called:

        ## Banner
        self.frm_banner_height = height*0.2
        self.frm_banner = tk.Frame(master=self.frm_pregen, width=self.frm_pregen_width, height=self.frm_banner_height)
        self.FrameBanner = FrameBanner(self.frm_banner, color_black, color_yellow, self.frm_pregen_width, self.frm_banner_height)

I then later do:

self.frm_banner.grid(column=0, row=0)

This is what it looks like, with the corner where I want the two images to be empty:

image

Any suggestions?

CodePudding user response:

It is because you did not call any layout function on self.FrameBanner:

...
self.FrameBanner.pack() # or whatever layout manager you want
self.frm_banner.grid(column=0, row=0)
...

CodePudding user response:

When initializing a widget, you should init with the master of the root. I think you can try removing

super(FrameBanner, self).__init__(root)

And change the widget init like below:

self.banner_STAR = tk.Label(master=root, image=self.image_STAR)
  • Related