Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Setting the position of TKinter labels
Next article icon

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:

Python3
importtkinterastkroot=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:

Add Shadow in Tkinter Label
 

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.

Python3
importtkinterastkroot=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:

Add Shadow in Tkinter Label
 

Example 2: 

Tkinter Label with Shadow nearby Lower-Left.

Python3
importtkinterastkroot=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:

Add Shadow in Tkinter Label
 

Example 3: 

Here, we will try to add Tkinter Label with Shadow around all Sides.

Python3
importtkinterastkroot=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:

Add Shadow in Tkinter Label
 

Example 4: 

In this example, we will see how we can change the shadow color.

Python3
importtkinterastkroot=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:

Add Shadow in Tkinter Label
 

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp