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
- activebackground : Background color when the mouse is over the label.
- activeforeground : The color of the text when the mouse is over the label.
- 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.
- background : (or bg) The background color of the label.
- bitmap : Give this option a bitmap or an image object and the label will be displayed with this graphic.
- borderwidth : (or bd) Thickness of the border around the label.
- 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.
- cursor : The mouse pointer when it hovers over the label.
- disabledforeground : The foreground color to display when the label is 'disabled'.
- 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.
- foreground : (or fg) Foreground color of the label. It will be used for text or for bits at 1 of the bitmap.
- height : height of the label in number of lines (not in pixels). If this option is not specified, the label adjusts to its content.
- highlightbackground : The color to highlight the focus when the widget has lost it.
- highlightcolor : The color to highlight the focus when the widget has it.
- highlightthickness : Thickness of the focus highlight line.
- image : To display an image in a label, specify an image object for this option.
- justify : Specify the alignment of the text: ‘left’ for left alignment, ‘center’ for center and ‘right’ for right alignment.
- padx : Additional horizontal space to insert left and right in the label. Its default value is 1.
- pady : Similar to padx but for the vertical direction.
- relief : Specifies the appearance of the decorative border around the label. By default, is 'flat'; for other values.
- 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).
- 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.
- 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.
- 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.
- 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.
- 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
my-courses.net