Tkinter Label Implementation: Display Text and Images with Labels

Tkinter Label Implementation

Tkinter Label allows the user to identify controls and provide visual feedback to the user. You can provide labels in various widgets like text, image, buttons e.t.c in Tkinter. In this entire tutorial, you will learn how to implement Tkinter Label by display text and images with labels.

Displaying Text with Labels

In this section, you will learn how to display and manipulate text with labels. You will learn to label the text inside the GUI, change the font color, background color, and many more. I am doing all these codes in Pycharm IDEs. Make sure you also do on it for better understanding.

Create Text with Label

from tkinter import *
from tkinter import ttk

root = Tk()
label = ttk.Label(root,text = "Welcome to Data Science Learner!")
label.pack()
root.mainloop()

Explanation of the Code

Here I am first creating the Root Gui using the Tk() method. After that, I used the Label() constructor with root and text value as an argument. It will be used to create label for the text. Then to display it on the screen you have to use geometry manager. Here I am using pack manager. Lastly to use and display the interface of Tkinter you have to mainloop() method in Pycharm. However, if you are using the command prompt and terminal then ignore it.
If you will run the code then you will get the following output.

Output

Tkinter Text Label
Tkinter Text Label

 

Add or Change Text

You can also change or add the content of text using the label.config(). Let’s add new text.

label.config(text ="Welcome to Data Science Learner! Here you will learn all about 
Data Science in an easy Way and it's for free" )

Output

Tkinter Text Label add new text content
Tkinter Text Label add new text content

Wrapping and Justifying the content

Let’s wrap and justify the content so the text on UI looks good.

label.config(wraplength = 200)
label.config(justify = "center")

Output

Wraping and justifying the text content
Wrapping and justifying the text content

Change Color of the text

You can also change the background and foreground color of the text using the label.config() method.  To do so you have to pass the foreground and background as an argument. Use the code given below.

label.config(foreground = "white",background="red")

Output

Changing the color of text
Changing the color of text

Change font of  the Text

You can also change the font style, size, and format using the config() method.

label.config(font=("Courier",20,"bold"))

Output

Styling the text
Styling the text

Displaying Image with Labels

Just like displaying the text using the Label() constructor you can also display an image with it. To do so you have to define the path of the image file and then pass it as an argument inside the Label widget. Execute the below lines of code and run it.

from tkinter import *
from tkinter import ttk

root = Tk()
logo = PhotoImage(file ="logo-dsl.png")
label = ttk.Label(root,image = logo)
label.config(background="blue")
label.pack()
root.mainloop()

Output

Displaying Image with Labels
Displaying Image with Labels

Conclusion

These are the implementation of the Tkinter Label. Tkinter Label widgets allow you to label anything you want like form, buttons, image e.t.c. Hope You have clearly understood is the label and how to use it in Tkinter. Even you have any doubt then you can contact us for more information.

Source:

Tkinter Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner