Creating a label widget

The Tk label widget is used to display information that does not allow direct user input. This information can be any alphanumeric information from simple labels for other controls to large instructions to a user.

The keywords are described in the Tk main pages, as follows:

Standard keyword

Interpretation

-activebackground

Specifies the background color to be used when drawing the element. The active background is the color used when the mouse is over the element and when pressing the mouse button will initiate an action.

-activeforeground

Specifies the foreground color to be used when drawing the element. The active foreground is the color used when the mouse is over the element and when pressing the mouse button will initiate an action.

-anchor

Specifies how the information within the widget is displayed. Acceptable values are:

n – north, top

ne– northeast, top right

e – east, right-hand side

se – southeast, bottom right

s – south, bottom

sw – southwest, bottom left

w – west, left-hand side

nw – northwest, top right

center - center

-background or bg

Specifies the background color to be used when drawing the element.

-bitmap

Specifies a bitmap to display within the element

-borderwidth or -bd

Specifies a non-negative value indicating the width of the 3D border to draw around the outside of the window.

-compound

Specifies if the button should display both an image and text and where the image should be displayed. Acceptable values are none, bottom, top, left, right, or center (default).

-cursor

Specifies the mouse cursor to be used for the window.

-disabledforeground

Specifies the color to use when displaying a disabled element.

-font

Specifies the font to use when drawing the element.

-foreground or fg

Specifies the normal foreground color to be used when drawing the element.

-highlightbackground

Specifies the color to display in the traversal highlight region when the window does not have the input focus.

-highlightcolor

Specifies the color to use for the traversal highlight rectangle that is drawn around the window when it has the input focus.

-highlightthickness

Specifies a non-negative value indicating the width of the highlight rectangle to draw around the outside of the window.

-image

Specifies the image to display within an element. The image must first have been created using the image create command.

-justify

When multiple lines of text exist, this keyword specifies the justification to apply within the element. Acceptable values are: left, center, or right.

-padx

Specifies a non-negative value indicating how much extra space to request for the window in the X-direction

-pady

Specifies a non-negative value indicating how much extra space to request for the window in the Y-direction.

-relief

Specifies the 3D effect desired for the window. Acceptable values are raised, sunken, flat, ridge, solid, and groove.

-takefocus

Determines whether or not the window accepts the focus during keyboard traversal.

-text

Specifies a string to be displayed within the element.

-textvariable

Specifies the name of a text variable that contains text to be displayed within an element.

-underline

Specifies the integer index of a character to be underlined, zero-based.

-wraplength

Specifies the maximum line length at which point the text will be wrapped for those elements that support word wrap.

-height

Specifies the desired height for the window

-state

Specifies the state of the widget. Acceptable values are normal, active, and disabled.

-width, width, Width

Specifies the desired width for the window.

How to do it…

In the following example, we will create a label with the desired text. Enter the following command:


1 % label .l -text "My Label"
.l

2 % pack .l

At this point, your window should look like the following:

How to do it…

How it works…

Based on the keywords provided, we have created a label widget named .l containing the text My Label.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.176