1

'''

import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from PIL import ImageTk, Image

root = tk.Tk()
root.geometry("1255x944")

toplevel_frame = ttk.Frame(root)
Title_frame = ttk.Frame(toplevel_frame)

img= tk.PhotoImage(file="logo.PNG")
img = img.subsample(5, 5)

lbl = tk.Label(Title_frame,image=img, compound = tk.LEFT)
lbl.image = img
lbl.grid(row=0,column=0,padx=20, pady=0)

root.mainloop()

''' It is not showing any error but I am unable to show image inside a frame

1 Answers1

1

As mentioned by @acw1668 in the comments, the frames namely toplevel_frame and Title_frame have not been rendered using any layout function(for more info).

The label with the image(i.e. lbl) is contained within the Title_frame which is within the toplevel_frame. So if the toplevel_frame and the Title_frame are not rendered any widget they contain also would not be rendered. Thus the label with the image(lbl) is not rendered.

This can simply be fixed by using any particular layout function to render these frames.

Using grid the two other lines to be added will look like this -:

toplevel_frame.grid(row=0,column=0)
Title_frame.grid(row=0,column=0)

NOTE: The two lines have to be added in the order they are defined, that is the frames have to be rendered in the order of their definition i.e. toplevel_frame frame first followed by the Title_frame frame followed by the lbl label. This will be more clear looking at the full code as provided below.

With the necessary changes done the full code will look something like this -:

# IMPORTS
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from PIL import ImageTk, Image

# ROOT INITIATION
root = tk.Tk()
root.geometry("1255x944")

# IMAGE OBJECT DEFINITION
img= tk.PhotoImage(file="logo.PNG")
img = img.subsample(5, 5)

# WIDGET DEFINITION
toplevel_frame = ttk.Frame(root)

Title_frame = ttk.Frame(toplevel_frame)

lbl = tk.Label(Title_frame,image=img, compound = tk.LEFT)
lbl.image = img

# LAYOUT MANAGEMENT
toplevel_frame.grid(row=0,column=0) # ADDED.
Title_frame.grid(row=0,column=0) # ADDED.
lbl.grid(row=0,column=0,padx=20, pady=0)

# STARTING THE ROOT MAINLOOP
root.mainloop()

NOTE: Further while deciding on the layout management function to use keep this in mind.

typedecker
  • 1,351
  • 2
  • 13
  • 25