Tkinter is the standard GUI library for Python and is included with most Python installations. It provides a number of widgets and tools for creating graphical user interfaces (GUIs) in Python. One of the most common widgets in Tkinter is the Label widget, which is used to display text and images. In this article, we will look at how to add a shadow effect to a Label 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 Tkinter Tk object and set the window size to 300×200 pixels using the geometry method.
import tkinter as tk root = 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 the place method to set the position of the shadow Label 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:
Python3
import tkinter as tk root = tk.Tk() root.geometry( "300x200" ) # Create label label = 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, the Label widget is used to display text and images. To create a shadow effect for a Label widget, we will create two Label widgets: one for the shadow and one for the actual text. We will place the shadow Label widget first with a grey background, and then place the text Label 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 two Label widgets, one for the shadow and one for the actual text. The shadow Label 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.
Python3
import tkinter as tk root = tk.Tk() root.geometry( "300x200" ) # Create shadow label shadow = tk.Label( root, text = "Geeks for Geeks" , font = ( "cambria" , 20 ), bg = "green" ) shadow.place( x = 15 , y = 5 ) # Create label label = 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.
Python3
import tkinter as tk root = tk.Tk() root.geometry( "300x200" ) # Create shadow label shadow = tk.Label( root, text = "Geeks for Geeks" , font = ( "cambria" , 20 ), bg = "green" ) shadow.place( x = 5 , y = 15 ) # Create label label = 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.
Python3
import tkinter as tk root = tk.Tk() root.geometry( "300x200" ) # Create shadow label shadow = 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 label label = 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.
Python3
import tkinter as tk root = tk.Tk() root.geometry( "300x200" ) # Create shadow label shadow = tk.Label( root, text = "Geeks for Geeks" , font = ( "cambria" , 20 ), bg = "lightgreen" ) shadow.place( x = 14 , y = 14 ) # Create label label = tk.Label( root, text = "Geeks for Geeks" , font = ( "cambria" , 20 ) ) label.place( x = 10 , y = 10 ) root.mainloop() |
Output: