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.
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" )
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")
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")
Change font of the Text
You can also change the font style, size, and format using the config() method.
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()
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.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.