Social Icons

Friday, March 6, 2020

The Label Widget

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

No comments:

Post a Comment

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (1) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (167) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (8) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) HTML (7) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (32) Python BeautifulSoup (1) Python For Data Science (2) Python-Books (6) Python-DVD-Training (1) Python-Exercises (138) Python-Framework (1) Python-IDE (1) Python-Modules (1) Python-pdf (2) Python-pyQt (1) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (1) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates