Tkinter is the standard GUI library for Python and is included with mostPython installations. It provides a number of widgets and tools for creating graphical user interfaces (GUIs) in Python. One of the most common widgets inTkinter is theLabel widget, which is used to display text and images. In this article, we will look at how to add a shadow effect to aLabel widget in Tkinter.
Required Modules:
If Tkinter is not installed, you can install it by running the following command.
pip install python-tk
Tkinter Label Without Shadow
Step 1: Import the required module
We first create a TkinterTk object and set the window size to300x200 pixels using the geometry method.
import tkinter as tkroot = tk.Tk()root.geometry("300x200")
Step 2: Create a label
We specify the text to be displayed as"Geeks for Geeks", set the font to"cambria" with a size of 20, and set the background color to"green". We then use theplace method to set the position of the shadowLabel widget 5 pixels to the right and down from the top left corner of the window.
label = tk.Label( root, text = "Geeks for Geeks", font = ("cambria", 20))label.place( x = 10, y = 10)
Example:
Python3importtkinterastkroot=tk.Tk()root.geometry("300x200")# Create labellabel=tk.Label(root,text="Geeks for Geeks",font=("cambria",20))label.place(x=10,y=10)root.mainloop()
Output:
Different Examples of Shadow in Tkinter Label
In Tkinter, theLabel widget is used to display text and images. To create a shadow effect for aLabelwidget, we will create twoLabelwidgets: one for the shadow and one for the actual text. We will place the shadowLabel widget first with a grey background, and then place the textLabel widget on top of it with the desired text and font. By making changes to the x and y values, we can change the shadow direction in Tkinter.
Create a shadow label
We can create twoLabel widgets, one for the shadow and one for the actual text. The shadowLabel widget is created and added with the following code:
shadow = tk.Label( root, text = "Geeks for Geeks", font = ("cambria", 20), bg = "green")shadow.place( x = 5, y = 5 )
Example 1:
Tkinter Label with Shadow nearby Upper-Right.
Python3importtkinterastkroot=tk.Tk()root.geometry("300x200")# Create shadow labelshadow=tk.Label(root,text="Geeks for Geeks",font=("cambria",20),bg="green")shadow.place(x=15,y=5)# Create labellabel=tk.Label(root,text="Geeks for Geeks",font=("cambria",20))label.place(x=10,y=10)root.mainloop()
Output:
Example 2:
Tkinter Label with Shadow nearby Lower-Left.
Python3importtkinterastkroot=tk.Tk()root.geometry("300x200")# Create shadow labelshadow=tk.Label(root,text="Geeks for Geeks",font=("cambria",20),bg="green")shadow.place(x=5,y=15)# Create labellabel=tk.Label(root,text="Geeks for Geeks",font=("cambria",20))label.place(x=10,y=10)root.mainloop()
Output:
Example 3:
Here, we will try to add Tkinter Label with Shadow around all Sides.
Python3importtkinterastkroot=tk.Tk()root.geometry("300x200")# Create shadow labelshadow=tk.Label(root,text="Geeks for Geeks",font=("cambria",20),bg="green")shadow.place(x=5,y=15)shadow=tk.Label(root,text="Geeks for Geeks",font=("cambria",20),bg="green")shadow.place(x=15,y=5)shadow=tk.Label(root,text="Geeks for Geeks",font=("cambria",20),bg="green")shadow.place(x=5,y=5)shadow=tk.Label(root,text="Geeks for Geeks",font=("cambria",20),bg="green")shadow.place(x=15,y=15)# Create labellabel=tk.Label(root,text="Geeks for Geeks",font=("cambria",20))label.place(x=10,y=10)root.mainloop()
Output:
Example 4:
In this example, we will see how we can change the shadow color.
Python3importtkinterastkroot=tk.Tk()root.geometry("300x200")# Create shadow labelshadow=tk.Label(root,text="Geeks for Geeks",font=("cambria",20),bg="lightgreen")shadow.place(x=14,y=14)# Create labellabel=tk.Label(root,text="Geeks for Geeks",font=("cambria",20))label.place(x=10,y=10)root.mainloop()
Output: