Python Label - What is TKinter Label in python?

Published December 12, 2021

A Tkinter label is used to build the display boxes used to display images or texts. The programmer/developer can change the picture or text attributes at any given moment using these widgets. Using these properties, you can control how the text or picture is shown on the screen. It's possible to execute numerous actions, such as underlining or spanning the whole text string, with this label.

In this post, we will learn how to implement the Tkinter Label widget in your Python program to display an image or text.

 

How to Create Tkinter label

The following syntax is used in the creation of this widget:

w = Label (root, option)

Where:

  • root- this  is the parent window of the label

  • Option- these are the options used for the widget. Several options can be used, separated with commas.

 

Tkinter label Options

  • Anchor- specifies where the text is positioned. This option can be used if the widget has a bigger area than the area required by texts.

  • Bg- specifies the  background color of the label

  • Bitmap- option  to display an image  or a graphic

  • Bd- specifies the border of the label.  By default, the border size is 2px.

  • Cursor- specifies the cursor option such as a dot, arrow, etc.

  • Font- specifies the  font of the text  displayed in the label

  • Fg - specifies the font color of the text displayed.

  • Height- specifies the vertical length of the new frame.

  • Image- displays the  static image  within the label

  • Justify- specifies how the text is aligned on the label, whether right, left, or center (default). 

  • Padx- specifies the extra space on the right and left sides of the text.

  • Pady- specifies the space added at the top and bottom of the label.  The default is usually FLAT.

  • Text-displays a line of text or multiple lines of text.

  • Textvariable -  slaves the text  displayed in the label

  • Underline- underlines the selected text in a label

  •  Width- specifies the length required to fit the contents in terms of characters.

Wraplength- limits the number of characters used in every line of the text.

 

An Example of a Tkinter Label

The following example displays the text, “Hello planet Earth, I am the next Zuckerburg”.

 

from tkinter import *

root = Tk()

var = StringVar()

label = Label(root, textvariable=var, relief=RAISED)

var.set("Hello planet Earth, I am the next Zuckerburg ")

label.pack()

root.mainloop()

 

 

Output:

Python Tkinter Label example

 

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

525 Views