Use an Image for the Background in Tkinter



Background images in tkinter are versatile as the functionality can be used in making 3D, 2D games, screensaver, desktop visualizations software, etc. Tkinter canvas is used to work with all these functionalities in an application.

Example

In this example, we will add a background image using the create_image() method in the canvas widget.

#Import the required library
from tkinter import *
from PIL import Image, ImageTk
from tkinter import ttk
#Create an instance of tkinter window
win= Tk()
#Define the geometry of the window
win.geometry("750x650")
#Load the image
bg= ImageTk.PhotoImage(file="./tutorialspoint.png")
#Create a canvas
canvas= Canvas(win,width= 400, height= 200)
canvas.pack(expand=True, fill= BOTH)
#Add the image in the canvas
canvas.create_image(0,0,image=bg, anchor="nw")
#Add a text in canvas
canvas.create_text(310,550,text="</Hello,Devs!_", font= ('Courier 45 bold'))
win.mainloop()

Output

Now, execute the above code to display a window that contains a background image with some text.

Updated on: 2021-04-16T07:06:46+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements