How to place an image into a frame in Tkinter?


To place an image into a Tkinter frame, you can follow the steps given below −

Steps −

  • Import the required libraries and create an instance of tkinter frame. To open an image and place it inside the frame, we will use the Pillow (PIL) library.

  • Set the size of the frame using geometry method.

  • Create a frame and specify its height and width. Place the frame at the center of the window using place() method with anchor='center'.

  • Open an image using ImageTk.PhotoImage("image"))

  • Next, create a label object inside the frame and pass the image inside the label.

  • Finally, run the mainloop of the application window


# Import required libraries
from tkinter import *
from PIL import ImageTk, Image

# Create an instance of tkinter window
win = Tk()

# Define the geometry of the window

frame = Frame(win, width=600, height=400)
frame.pack()'center', relx=0.5, rely=0.5)

# Create an object of tkinter ImageTk
img = ImageTk.PhotoImage("forest.jpg"))

# Create a Label Widget to display the text or Image
label = Label(frame, image = img)



When we run the above code, it will display the following output −

Published on 26-Oct-2021 13:26:46