Welcome folks today in this blog post we will be setting the background image of label widget using the pillow library in python. All the full source code of the application is shown below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 |
# importing required packages import tkinter from PIL import ImageTk, Image # creating main window root = tkinter.Tk() # running the application root.mainloop() |
As you can see in the above code we are importing the pillow
library and then we are also importing the tkinter library. And then we are initializing the tkinter
window and then we will be running the app using the mainloop()
method.
1 2 |
# loading the image img = ImageTk.PhotoImage(Image.open("profile.jpg")) |
As you can see we are using the photoImage()
method and inside it we are setting the path of the image. This image can be PNG or JPEG.
Now we will be adding the image
as the background of the label widget using the label
method and then we are also adding it to the window using the pack()
method.
1 2 3 4 5 6 |
# reading the image panel = tkinter.Label(root, image = img) # setting the application panel.pack(side = "bottom", fill = "both", expand = "yes") |