Label widgets are used to display one or more lines of text with a same style, a bitmap, or an image with the ability to customize the appearance  of the text and background..

1 - How to create a Label ?

To create a label in a main window or in a parent frame we use the following syntax:

label_name = Label (parent_window, option, ...)

Example of creating a label

from tkinter import *
# create parent window
myWindow = Tk()

# Creation of the label
myLabel = Label (myWindow, text = "here is my First Label")
# Place the label on the window
myLabel.pack()

myWindow.mainloop ()

The output is:

2 - Label Widget options


The Label widget has many options for changing its appearance, such as the text color, background color, size and type of font that will be used to display the text ...
Here is the list options available for the Label Tkinter widget

  1. activebackground : Background color when the mouse is over the label. 
  2. activeforeground : The color of the text when the mouse is over the label. 
  3. anchor : This option specifies the position of the text if the widget has more space than necessary for the text. The default is 'center', which has the effect of centering the text in relation to the available space. For other values, see The anchoring system. For example, if you use anchor = 'nw', the text will be positioned in the upper left corner of the available space. 
  4. background : (or bg) The background color of the label.
  5. bitmap : Give this option a bitmap or an image object and the label will be displayed with this graphic.
  6. borderwidth : (or bd) Thickness of the border around the label.
  7.  compound : If you want the ethic to display both text and a graphic (either a bitmap or an image), this option is used to specify the relative orientation of the image in relation to the text. The values ​​can be 'left', 'right', 'center', 'bottom' or 'top'. For example, if compound = BOTTOM, the graph will be displayed below the text. 
  8. cursor : The mouse pointer when it hovers over the label.
  9. disabledforeground : The foreground color to display when the label is 'disabled'. 
  10. font : If you display text in this label (with the text or textvariable option), this option is used to specify the font used to display the text.
  11. foreground : (or fg) Foreground color of the label. It will be used for text or for bits at 1 of the bitmap.
  12. height : height of the label in number of lines (not in pixels). If this option is not specified, the label adjusts to its content. 
  13. highlightbackground : The color to highlight the focus when the widget has lost it. 
  14. highlightcolor : The color to highlight the focus when the widget has it. 
  15. highlightthickness : Thickness of the focus highlight line. 
  16. image : To display an image in a label, specify an image object for this option.
  17. justify : Specify the alignment of the text: ‘left’ for left alignment, ‘center’ for center and ‘right’ for right alignment. 
  18. padx : Additional horizontal space to insert left and right in the label. Its default value is 1. 
  19. pady : Similar to padx but for the vertical direction. 
  20. relief : Specifies the appearance of the decorative border around the label. By default, is 'flat'; for other values.
  21. state : By default, a label is in the "normal" state. The other possible states are ‘disabled’ and ‘active’ (the background and foreground colors for these states are then used). 
  22. takefocus : Normally, a label does not get focus; see The focus: reception of keyboard input. If you want the label to receive it, put 1 for this option. 
  23. text : To display one or more lines of text in a label, specify a character string that contains the text. The special character ' n' will force a line break. 
  24. textvariable : To be able to vary the displayed text at the same time as the value of a StringVar type control variable, set this option with this variable. See Control variables: Values ​​under the widgets. 
  25. underline : You can underline one of the characters in the text by indicating its position (from 0). By default, underline = :1, which means no underline. width : Width of the label expressed in number of characters (not in pixels). If this option is not specified, the label adjusts to its content. 
  26. wraplength : You can limit the number of characters on each line by specifying it for this option. The default value is 0, which means that the lines will be cut only if there is a line break.

Example

from tkinter import *
# create parent window
myWindow = Tk()
myWindow.geometry("250x150")

# Create a label with options
myLabel = Label (myWindow,
text = "here is my First Label" ,
font = "Broadway",
bd = 20,
relief = RAISED,
bg = "white" ,
fg = "blue")
# Place the label on the window
myLabel.pack()

myWindow.mainloop ()

The out put is:

To see more details for the label options visit the official web site : https://docs.python.org/3/library/tk.html

Younes Derfoufi
my-courses.net

Leave a Reply