pythonpython-3.xtkintertkinter-canvastkinter-label

Tkinter Image not displaying in label or canvas


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 1 Banner
        # self.banner_1 = tk.Canvas(master=self, width=width*0.8, height=height, bg=color_yellow)
        self.image_1 = ImageTk.PhotoImage(Image.open("images_2/logo_here.png").resize((round(width*0.7), round(height*0.9)), Image.NEAREST))
        # self.banner_1.create_image(10, 10, anchor="w", image=self.image_1)
        # self.banner_1.image = self.image_1
        self.banner_1 = tk.Label(master=self, image=self.image_1)
        self.banner_1.image = self.image_1

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

        # Grid Layout
        self.banner_1.grid(column=0, row=0, sticky='w')
        self.banner_2.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?


Solution

  • 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)
    ...