Python Tkinter Label Widget
In this tutorial, we will cover the Tkinter Label widget in Python, which is used to create a Label in the GUI application in which we can show any text or image.
The label widget in Tkinter is used to display boxes where you can place your images and text.
-
The label widget is mainly used to provide a message about the other widgets used in the Python Application to the user.
-
You can change or update the text inside the label widget anytime you want.
-
This widget uses only one font at the time of displaying some text.
-
You can perform other tasks like underline some part of the text and you can also span text to multiple lines.
-
There are various options available to configure the text or the part of the text shown in the Label.
Tkinter Label Widget
The syntax of the label widget is given below,
W = Label(master,options)
In the above syntax, the master
parameter denotes the parent window. You can use many options
to configure the text and these options are written as comma-separated key-value pairs.
Tkinter Label Widget Options
Following are the options used with label widgets:
Name of the option |
Description |
anchor |
This option is mainly used for controlling the position of text in the provided widget size. The default value is CENTER which is used to align the text in center in the provided space. |
bd |
This option is used for the border width of the widget. Its default value is 2 pixels. |
bitmap |
This option is used to set the bitmap equals to the graphical object specified so that now the label can represent the graphics instead of text. |
bg |
This option is used for the background color of the widget. |
cursor |
This option is used to specify what type of cursor to show when the mouse is moved over the label. The default of this option is to use the standard cursor. |
fg |
This option is used to specify the foreground color of the text that is written inside the widget. |
font |
This option specifies the font type of text inside the label. |
height |
This option indicates the height of the widget |
image |
This option indicates the image that is shown as the label. |
justify |
This option specifies the alignment of multiple lines in the label. The default value is CENTER. Other values are RIGHT, LEFT; you can justify according to your requirement |
padx |
This option indicates the horizontal padding of the text. The default value of this option is 1. |
pady |
This option indicates the vertical padding of the text. The default value of this option is 1. |
relief |
This option indicates the type of border. The default value of this option is FLAT |
text |
This option is set to the string variable and it may contain one or more than one line of text |
textvariable |
This option is associated with a Tkinter variable that is (StringVar) with a label. If you change the value of this variable then text inside the label gets updated. |
underline |
This option is used to underline a specific part of the text. The default value of this option =-1(no underline); you can set it to any integer value up to n and counting starts from 0. |
width |
This option indicates the width of the widget. |
wraplength |
Rather than having only one line as the label text, you can just break it to any number of lines where each line has the number of characters specified to this option. |
Tkinter Label Widget Example
Now let us see a basic example of the label widget and the code snippet is given below:
import tkinter
from tkinter import *
win = Tk()
var = StringVar()
label = Label( win, textvariable=var, relief=RAISED )
# set label value
var.set("Hey!? Welcome to StudyTonight")
label.pack()
win.mainloop()
In the above code, we created a simple variable StringVar()
and then assigned a value to it, and this variable is assigned as value to the textvariable
option of the Label widget.
Tkinter Label Widget - Another Example
Below we have another code snippet for more clear understanding. Let us see the code snippet given below:
from tkinter import *
win = Tk()
win.geometry("400x250")
#creating a label
username = Label(win, text = "Username").place(x = 30,y = 50)
#creating second label
password = Label(win, text = "Password").place(x = 30, y = 90)
submitbutton = Button(win, text = "Submit",activebackground = "red", activeforeground = "blue").place(x = 30, y = 120)
e1 = Entry(win,width = 20).place(x = 100, y = 50)
e2 = Entry(win, width = 20).place(x = 100, y = 90)
win.mainloop()
Whenever you will run the above code, after putting values into username and password label, when you will click on the submit button then its color gets changed to red.
Don't worry about the Button Widget and Entry Widget used in the above code, we will cover them shortly in the upcoming tutorials. This example is to give you an idea about how Tkinter widgets are used to create user interfaces for your Tkinter application.
Summary:
In this tutorial, we covered the Tkinter Label Widget which is used to show text and images in Tkinter GUI application or to add texts with form input fields like we have done in the example above.