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.open("image"))

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

  • Finally, run the mainloop of the application window

Example

# 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
win.geometry("700x500")

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

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

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

win.mainloop()

Output

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

Updated on: 22-Aug-2023

61K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements