-3

I was wondering if it's possible to set a background image on a frame in Tkinter. I tried by setting a canvas inside a Frame, with an image, but it did not succeed to import.

def background(self):
    my_background = Canvas(self.__Frame4, bg="black")
    filename = PhotoImage(file=r"images\main_bg.png")
    background_label = Label(self.__Frame4, image=filename)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)
    my_background.pack(expand=1, fill=BOTH)
martineau
  • 119,623
  • 25
  • 170
  • 301
elisac
  • 1

1 Answers1

0

Here is an example of imported image in a Label :

import tkinter as tk
from PIL import ImageTk, Image

def create_img(frame, path_to_img):
    width = 100
    height = 140

    img = Image.open(path_to_img)
    img = img.resize((width,height), Image.ANTIALIAS)
    photoImg =  ImageTk.PhotoImage(img)

    img_label = tk.Label(frame, image=photoImg, width=width)
    img_label.image = photoImg
    return img_label

root = tk.Tk()
root.grid_rowconfigure(0, weight=1, minsize=1)
root.grid_columnconfigure(0, weight=1, minsize=1)

my_image = create_img(root, "images\main_bg.png")
my_image.grid(row=1, column=0)

root.mainloop()
Phoenixo
  • 2,071
  • 1
  • 6
  • 13