How to Use Images as Backgrounds in Tkinter?

PythonTkinterGUI-Programming

If we will create an instance of Tkinter frame and display the window while keep running it, then it will show the default output canvas. However, we can add an image inside the Tkinter canvas as a background using PhotoImage methods and Canvas methods.

Since image support in Tkinter is limited to Gif, PNG and PPM, the PhotoImage(GIF, PNG, PPM) function takes the location of the image file and displays the canvas with the image as a background.

First, we will create a PhotoImage Object using the PhotoImage function.

Example

from tkinter import *
from PIL import ImageTk

win = Tk()
win.geometry("700x300")

#Define the PhotoImage Constructor by passing the image file
img= PhotoImage(file='down.png', master= win)
img_label= Label(win,image=img)

#define the position of the image
img_label.place(x=0, y=0)

win.mainloop()

Output

Running the above code snippet will display a window with a background image.

raja
Published on 06-Mar-2021 13:15:58
Advertisements