In this post we will learn in-depth on the label, how to use them, what parameters to use and what arguments to pass. Lets get started,
IN[1]
import tkinter as tk
from tkinter import ttk
try :
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
except :
pass
root = tk.Tk()
root.geometry("600x400")
root.resizable(False, False)
root.title("Labels in Tkinter")
root.mainloop()
OUT[1]
First we will create a simple window as root. Using geometry function we provide 600x400 width and height to root window. To avoid window expansion, passing False as an argument to resizable method then using title method we provide title to the root window which is Labels in Tkinter.
IN[2]
import tkinter as tk
from tkinter import ttk
try :
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
except :
pass
root = tk.Tk()
root.geometry("600x400")
root.resizable(False, False)
root.title("Labels in Tkinter")
label = ttk.Label(root, text = "Welcome", padding = 20)
label.config(font=("Segoe UI", 20))
label.pack()
root.mainloop()
OUT[2]
Now lets create a new label and pass in our text "Welcome". Using config method we will configure font style and font size.
IN[3]
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
try :
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
except :
pass
root = tk.Tk()
root.geometry("600x400")
root.resizable(False, False)
root.title("Labels in Tkinter")
image = Image.open("D:\Learning\Python\Code\Python\Python Tkinter\Phasinee-Boonrod-01.jpg")
photo = ImageTk.PhotoImage(image)
label = ttk.Label(root, image = photo, padding = 5)
label.pack()
root.mainloop()
OUT[3]
Now lets try to import image into the label, to do that we have to import Pillow package. Lets first see how to install Pillow package, open command prompt and enter the bellow command
pip install pillow
Then import Image, ImageTk from PIL. Then pass the absolute path of the image to the Open function, then pass it to PhotoImage function and finally pass its variable to the label.
IN[4]
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
try :
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
except :
pass
root = tk.Tk()
root.geometry("600x400")
root.resizable(False, False)
root.title("Labels in Tkinter")
image = Image.open("D:\Learning\Python\Code\Python\Python Tkinter\Rin-Tachibana-02.jpg").resize((400, 350))
photo = ImageTk.PhotoImage(image)
label = ttk.Label(root, image = photo, padding = 5)
label.pack()
root.mainloop()
OUT[4]
In earlier output the photo has been placed but the image in not displayed properly due to fixed root window's height-width ratio and label has loaded entire picture with original resolution. To fix that we add resize method along with open method. You can pass in the resolution in which image wants to be displayed onto the resize method.
IN[5]
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
try :
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
except :
pass
root = tk.Tk()
root.geometry("600x400")
root.resizable(False, False)
root.title("Labels in Tkinter")
image = Image.open("D:\Learning\Python\Code\Python\Python Tkinter\Rin-Tachibana-02.jpg").resize((400, 350))
photo = ImageTk.PhotoImage(image)
label = ttk.Label(root, text = "Rin Tachibana ", image = photo, padding = 5, compound = "right")
label.pack()
root.mainloop()
OUT[5]
We can pass both text and picture simultaneously to label, just pass text argument along with the compound argument which has following parameters like right, left, top and bottom. If you pass only the text with compound to label then only the image will be displayed.
Points to Remember
Change a label's font with font = ("font name", font size).
Add an image by first opening it image = Image.open("absolute path to the image") then creating an ImageTk ImageTk.PhotoImage(image) and applying it to the label with image = photo.
Adjust text and image positioning with compound = "right".