Tkinter PhotoImage
Images can be applied to many places, such as labels, function buttons, option buttons, text areas, etc.
You can create an image object with the PhotoImage( ) method before using it, and then apply this object to other window components.
The syntax of Tkinter PhotoImage is as follows:
imageobj = PhotoImage(file="apidemos.gif") # The extension gif, passed back to the image object
Note that the PhotoImage( ) method only supports the gif file format in the early days, and does not accept images in the commonly used jpg or png formats, but currently supports the png format. It is recommended to place the gif image in the program’s folder for ease of use.
You can set this image object in the Label( ) method using the image=imageobj
parameter.
Tkinter PhotoImage Example#1
Window showing the basic application of apidemos.gif image.
"""
Set the image displayed by the control
"""
from tkinter import *
root = Tk()
root.title("apidemos.com")
html_gif = PhotoImage(file="apidemos.gif")
label = Label(root,image=html_gif) # Set the image displayed by the widget control
label.pack() # Packaging and positioning components
root.mainloop()
Output:
If you want to display jpg files inside the tab, you need to use the Image and ImageTk modules of the PIL module, please import the pillow module first, as shown below.
pip install pillow
Note that it is the PIL module that needs to be imported in the programming, mainly because of compatibility with older versions of the Python Image Library, as shown below.
from PIL import Image, ImageTk
Tkinter PhotoImage Example#2
Displaying apidemos.png inside a Label.
"""
Set the image displayed by the control
"""
from tkinter import *
from PIL import Image,ImageTk
root = Tk()
root.title("apidemos.com")
root.geometry("680x400")
image = Image.open("apidemos.png")
# image = Image.open("1.jpg")
yellowstone = ImageTk.PhotoImage(image)
label = Label(root,image=yellowstone) # Set the image displayed by the widget control
label.pack() # Packaging and positioning components
root.mainloop()
Output:
You can refer to Tkinter Widget compound Parameter Use the compound parameter to make the image coexist with the text label.
Tkinter PhotoImage Example#3
Application with both text labels and images in the window.
"""
Set the image and text displayed by the control
"""
from tkinter import *
from PIL import Image,ImageTk
root = Tk()
root.title("apidemos.com")
# root.geometry("680x400")
costomizedText = """welcome to apiodemos.com
welcome to apiodemos.com
welcome to apiodemos.com
welcome to apiodemos.com
welcome to apiodemos.com"""
image = Image.open("apidemos.png")
# image = Image.open("1.jpg")
yellowstone = ImageTk.PhotoImage(image)
label = Label(root,text=costomizedText,image=yellowstone,
bg="lightyellow",compound="left")
label.pack()
root.mainloop()
Output:
As you can see from the execution results above, the text label is center-aligned by default for the 2nd line of output. We can add the justify=LEFT parameter inside the Label( ) method to make the 2nd line of data output to the left.
Tkinter PhotoImage Example#4
Add the parameter justify="left" in line 10 to output the second line of the text label to the left, and to display the image to the right of the text label.
"""
Set the image and text displayed by the control
"""
from tkinter import *
from PIL import Image,ImageTk
root = Tk()
root.title("apidemos.com")
# root.geometry("680x400")
costomizedText = """welcome to apiodemos.com
welcome to apiodemos.com
welcome to apiodemos.com
welcome to apiodemos.com
welcome to apiodemos.com"""
image = Image.open("apidemos.png")
# image = Image.open("1.jpg")
yellowstone = ImageTk.PhotoImage(image)
label = Label(root,text=costomizedText,image=yellowstone,
justify="left",compound="right")
label.pack()
root.mainloop()
Output:
The last thing to remember is that the bitmap parameter and the image parameter cannot coexist, and if this happens, the bitmap parameter will not work.
Tkinter PhotoImage Example#5
The image coexists with the text, which is overlaid on top of the image.
"""
Set the image and text displayed by the control
"""
from tkinter import *
from PIL import Image,ImageTk
root = Tk()
root.title("apidemos.com")
# root.geometry("680x400")
costomizedText = """welcome to apiodemos.com
welcome to apiodemos.com
welcome to apiodemos.com
welcome to apiodemos.com
welcome to apiodemos.com"""
image = Image.open("apidemos.png")
# image = Image.open("1.jpg")
yellowstone = ImageTk.PhotoImage(image)
label = Label(root,text=costomizedText,image=yellowstone,
bg="lightyellow",compound="center")
label.pack()
root.mainloop()
Output: