SoFunction
Updated on 2024-10-30

Example of the Label component in the TKinter module in Python.

Python2.7.4 OS—W7x86

1. Introduction

Label is used to display text and images in a specified window. The final rendering of the Label is a content consisting of a background and foreground overlay.

Label component definition function: Label (master=None, cnf={}, **kw) where the kw parameter is the key-value pair used to customize the lable component.

2. Background customization

The background, then, has three components: content area + fill area + border

<1> content area parameters are: width,length used to specify the size of the area, if the content of the foreground of the display is text, then the size of a single character as a unit; if the display is an image, then the unit of pixels. The default value is dynamically adjusted according to the specific content displayed. The type is int.

background is used to specify the color of the background, the default value depends on the system.

<2> Fill area parameter: refers to the size of the interval between the content area and the border, in pixels. Parameters are: padx , pady, type is int.

<3> border parameters: stylerelief (optional values: flat (default), sunken, raised, groove, ridge), borderwidth (the width of the border in pixels, the default depends on the system, usually 1 or 2 pixels)

highlightbackground,highlightcolor,highlightthickness The three border parameters are used to set the highlight border color and highlight border width before and after focus acquisition only if the Label is allowed to receive focus (tackfocus=True).

To give you a chestnut (@-@)

On the right side of the image above, the background image consists of: content area (black), fill area (green), border (yellow)

The defined background content area is a 3X9 character area, such as the Label in the small window on the right side of the above figure, and the effect after adding the fill area and border is as the Label on the left side of the above figure.

3. Prospective customization

The foreground definition is divided into two subsections to illustrate textual content and images.

3.1 Text

Text content options are: <1> specify the font and font size, such as: font = (font_name,size), the default has the system to specify.

<2> text alignment, justify = "center(default) left/right/"

<3> specify the text (or image) color, foreground = "specified color", can be an English name, can also be in RGB format

<4> Specify text content: (static) text = "target string ....." ; (dynamically updated) textvariable = str_obj, which updates the corresponding content in Label when the content of str_obg changes.

It is important to note here that str_obj must be a string type variable supported by TKinter, e.g. str_obj = () str_obj.set("target text content")

<5> A single character adds an underscore, underline = index, and index is the character index value in the target string.

<6> the position of the text or image in the background content area: anchor Optional values are (n,s,w,e,ne,nw,sw,se,center) eswn is the first letter of the English language in the southeast, northwest and north, that is: on the north and south of the south of the left, west, right, east

The image content options are: <1> Specify image: bitmap = bitmap_image, this parameter is ignored when the image option is specified or image = normal_image (only images in GIF, PPM/PGM formats are supported)".

It should be noted that the image objects bitmap_image normal_image used here are all image formats that need to be converted by TKinter.

e.g. bitmap_image = (file = "bitmap path")

normal_image = (file = "gif , ppm/pgm image path")

Image and text trade-offs: The compound parameter controls the text and images to be displayed. When both text and images are specified to be displayed, the parameter can be set differently.

Optional value: None Default value, means only display the image, no text; bottom/top/left/right, means the image is displayed under/over/left/right of the text; center, means the text is displayed above the center of the image.

Compound="bottom" on the left of the above image means that the image is displayed below the text; compound="center" on the left of the above image means that the text is displayed above the center of the image.

4. Other parameters of Label

<1>activebacakground activeforground Used to set the background and foreground color of the Label when it is in active state, the default is specified by the system.

<2>diableforground Specifies the foreground color when the Label is not available (Disable), specified by the system by default.

<3>cursor Specifies the style of the mouse when it passes over the Label, which is specified by the system by default.

<4>state Specifies the state of the Label, used to control how the Label is displayed. The available values are: normal(default)/active/disable.

program source code (computing)

#coding=utf-8
import Tkinter as tk
 
if __name__ == "__main__":
  import Tkinter as tk
  master = ()
  str_obj = ()
  str_obj.set("This is the type of string supported by TKinter.")
 
  #bitmap_image = (file = "./tmp/")
  normal_image = (file = "./tmp/")
  print type(normal_image)
  print normal_image
  w = (master,
         #Background options
         #height = 5,
         #width = 20,
         padx=10,
         pady=20,
         background="blue",
         relief="ridge",
         borderwidth=10,
         # Text
         text = "123456789\nabcde\nABCDEFG",
         #textvariable = str_obj,
         justify = "left",
         foreground = "white",
         underline = 4,
         anchor = "ne",
         #image
         image = normal_image,
         compound = "bottom",
         #Receive the spotlight
         #takefocus = True,
         #highlightbackground = "yellow",
         #highlightcolor = "white",
         #highlightthickness = 5
         )
  ()
  ()

Above this on Python in the TKinter module in the Label component example details is all I share with you, I hope to be able to give you a reference, and I hope you support me more.