beetlebug▾
/

label

Makes a label: a widget that shows a text, an image, or both, and runs no script.

label PATHNAME ?OPTION VALUE ...?

Description

Makes a label widget with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. The label shows its -text, the value of its -textvariable, or its -image. It runs no command on a click: a script can bind one with bind.

The parent of PATHNAME must exist, and PATHNAME must not. A label is a Label in CSS and for the option database, and ztk::class gives it a style class for a stylesheet.

The theme draws the border and the padding, so -borderwidth, -padx, and -pady default to 0, and a value of the script overrides the theme. Some options for the look of Tk on X11 are stored with no effect in the modern theme, such as -highlightthickness and -activebackground. Differences from Tk lists them, and the theme classic has the look of Tk on X11.

An option needs its full name or a short form of its own, such as -bg for -background: -tex is an unknown option, in ztk and in Tk 8.6.16.

Widget commands

CommandDescription
label cgetGives the value of an option of the label.
label configureGives or sets the options of the label.

Options

OptionTypeDefaultDatabase nameDescription
-activebackgroundcolor#ecececactiveBackgroundThe background color of the active state, in the look of Tk on X11.
-activeforegroundcolor#000000activeForegroundThe text color of the active state, in the look of Tk on X11.
-anchoranchorcenteranchorThe place of the text or the image in a label larger than it.
-backgroundcolor#d9d9d9backgroundThe background color of the label.
-bitmapbitmap nameemptybitmapA bitmap name of Tk to show in place of the text.
-borderwidthscreen distance0borderWidthThe width of the border, in place of the border of the theme.
-compoundcompoundnonecompoundShows the image and the text together, with the image at that side.
-cursorcursor nameemptycursorThe shape of the pointer over the label.
-disabledforegroundcolor#a3a3a3disabledForegroundThe text color while the state is disabled.
-fontfontTkDefaultFontfontThe font of the text.
-foregroundcolor#000000foregroundThe color of the text.
-heightinteger0heightThe height of the label in lines of text, or in pixels for an image.
-highlightbackgroundcolor#d9d9d9highlightBackgroundThe color of the focus ring while the label has no focus, in the look of Tk on X11.
-highlightcolorcolor#000000highlightColorThe color of the focus ring while the label has the focus, in the look of Tk on X11.
-highlightthicknessscreen distance0highlightThicknessThe width of the focus ring, in the look of Tk on X11.
-imageimage nameemptyimageAn image to show in place of the text.
-justifyjustificationcenterjustifyThe alignment of the lines of a text of more than one line.
-padxscreen distance0padXThe space at the left and the right of the content, in place of the padding of the theme.
-padyscreen distance0padYThe space above and below the content, in place of the padding of the theme.
-reliefreliefflatreliefThe 3-D look of the border.
-statestatenormalstateThe look of the label: normal, active, or disabled.
-takefocusboolean, empty, or script0takeFocusWhether the Tab key can move the focus to the label.
-textstringemptytextThe text of the label.
-textvariablevariable nameemptytextVariableA global variable whose value is the text of the label.
-underlineinteger-1underlineThe index of a character of the text to underline, for a keyboard shortcut.
-widthinteger0widthThe width of the label in characters of the font, or in pixels for an image.
-wraplengthscreen distance0wrapLengthThe longest line of the text before it breaks into a new line.

-activebackground COLOR

The background color of the active state, in the look of Tk on X11.

The background of the label while -state is active. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB. The modern theme stores the value with no effect.

% label .ab -activebackground #ff0000
> .ab cget -activebackground
#ff0000
% label .ab2 -activebackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-activeforeground COLOR

The text color of the active state, in the look of Tk on X11.

The color of the text while -state is active. The class in the option database is Background, as in Tk.

% label .af -activeforeground white
> .af cget -activeforeground
white
% label .af2 -activeforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-anchor ANCHOR

The place of the text or the image in a label larger than it.

When the label is larger than its text or image, such as with -width or a geometry manager that stretches it, the anchor puts the content at that side or corner. center puts it in the middle.

Values: n, ne, e, se, s, sw, w, nw, center.

label with -text Name -width 14 and -anchor w-anchor wlabel with -text Name -width 14 and -anchor center-anchor centerlabel with -text Name -width 14 and -anchor e-anchor e
Each with -text Name -width 14.
% label .an -text Go -anchor w
> .an cget -anchor
w
% label .an2 -anchor middle
bad anchor "middle": must be n, ne, e, se, s, sw, w, nw, or center
-errorcode TCL LOOKUP INDEX anchor middle

-background COLOR

The background color of the label.

The color behind the text. -bg is the same option. With no value from the script, cget gives the color of the theme, which can differ from the default of the option database: .b configure -background lists both.

label with -text Name and -background {#fde68a}-background {#fde68a}label with -text Name and -background {#bfdbfe}-background {#bfdbfe}
Each with -text Name.
% label .bg -bg red
> .bg cget -background
red

# The default of the option database.
% label .bg3
> lindex [.bg3 configure -background] 3
#d9d9d9
% label .bg2 -background nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-bitmap BITMAP

A bitmap name of Tk to show in place of the text.

The name of a built-in bitmap of Tk: error, gray12, gray25, gray50, gray75, hourglass, info, questhead, question, or warning. A value that starts with @ names a file. ztk stores the value for cget and shows no bitmap.

% label .bm -bitmap error
> .bm cget -bitmap
error
% label .bm2 -bitmap nobitmap
bitmap "nobitmap" not defined
-errorcode TK LOOKUP BITMAP nobitmap

-borderwidth DISTANCE

The width of the border, in place of the border of the theme.

The width of the border around the label. -bd is the same option. The default 0 leaves the border to the theme, and a value of the script replaces it. A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.

label with -text Name -relief solid and -borderwidth 0-borderwidth 0label with -text Name -relief solid and -borderwidth 1-borderwidth 1label with -text Name -relief solid and -borderwidth 4-borderwidth 4
Each with -text Name -relief solid.
% label .bw -text Save
> label .bw6 -text Save -borderwidth 6
> expr {[winfo reqwidth .bw6] > [winfo reqwidth .bw]}
1

% label .bd -bd 3
> .bd cget -borderwidth
3
% label .bw2 -borderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS

-compound PLACE

Shows the image and the text together, with the image at that side.

With none, a label with an -image shows only the image. Each other value shows the image and the text, with the image at that side of the text, or under the text with center.

Values: none, bottom, center, left, right, top.

label with -text Name -image ::ref::icon and -compound none-compound nonelabel with -text Name -image ::ref::icon and -compound center-compound centerlabel with -text Name -image ::ref::icon and -compound top-compound toplabel with -text Name -image ::ref::icon and -compound bottom-compound bottomlabel with -text Name -image ::ref::icon and -compound left-compound leftlabel with -text Name -image ::ref::icon and -compound right-compound right
Each with -text Name -image ::ref::icon.
% image create photo pic -width 40 -height 20
> label .ci -image pic -text Save
> label .cl -image pic -text Save -compound left
> expr {[winfo reqwidth .cl] > [winfo reqwidth .ci]}
1
% label .co -compound sideways
bad compound "sideways": must be bottom, center, left, none, right, or top
-errorcode TCL LOOKUP INDEX compound sideways

-cursor CURSOR

The shape of the pointer over the label.

A cursor name of Tk, such as hand2, as the first word of a list. The names are the names of the cursor font of X and the names of Tk on Windows, such as size_we, in any case. A value that starts with @ names a file. An empty value uses the cursor of the parent.

% label .cu -cursor hand2
> .cu cget -cursor
hand2
% label .cu2 -cursor nocursor
bad cursor spec "nocursor"
-errorcode TK VALUE CURSOR

-disabledforeground COLOR

The text color while the state is disabled.

The color of the text while -state is disabled.

label with -text Name -state disabled and -disabledforeground {#888888}-disabledforeground {#888888}label with -text Name -state disabled and -disabledforeground {#b91c1c}-disabledforeground {#b91c1c}
Each with -text Name -state disabled.
% label .df -disabledforeground gray50
> .df cget -disabledforeground
gray50
% label .df2 -disabledforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-font FONT

The font of the text.

A font name, such as TkDefaultFont, or a font description, such as {Helvetica 12 bold} or {-family Helvetica -size 12}. A larger font makes a larger label. A family that does not exist falls back to a font of the system.

label with -text Name and -font TkDefaultFont-font TkDefaultFontlabel with -text Name and -font TkFixedFont-font TkFixedFontlabel with -text Name and -font {Helvetica 16 bold}-font {Helvetica 16 bold}
Each with -text Name.
% label .fo -text Save
> label .fo2 -text Save -font {Helvetica 24}
> expr {[winfo reqheight .fo2] > [winfo reqheight .fo]}
1

% label .fo3 -font {Helvetica 12 bold}
> .fo3 cget -font
Helvetica 12 bold
% label .fo4 -font {Helvetica 12 weird}
unknown font style "weird"
-errorcode TK LOOKUP FONT_STYLE weird

% label .fo5 -font {Helvetica big}
expected integer but got "big"
-errorcode TCL VALUE NUMBER

% label .fo6 -font {-family Helvetica -weight heavy}
bad -weight value "heavy": must be normal, or bold
-errorcode TK LOOKUP -weight heavy

% label .fo7 -font {}
font "" doesn't exist
-errorcode TK LOOKUP FONT {}

-foreground COLOR

The color of the text.

The color of the text and of a bitmap. -fg is the same option.

label with -text Name and -foreground {#b91c1c}-foreground {#b91c1c}label with -text Name and -foreground {#2563eb}-foreground {#2563eb}
Each with -text Name.
% label .fg -fg #123456
> .fg cget -foreground
#123456
% label .fg2 -foreground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-height LINES

The height of the label in lines of text, or in pixels for an image.

For a label with text, the height in lines of the font. For a label with an image, the height in pixels. 0 makes the label as tall as its content.

label with -text Name and -height 0-height 0label with -text Name and -height 2-height 2label with -text Name and -height 3-height 3
Each with -text Name.
% label .he -text Save
> label .he3 -text Save -height 3
> expr {[winfo reqheight .he3] > 2 * [winfo reqheight .he]}
1
% label .he2 -height x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-highlightbackground COLOR

The color of the focus ring while the label has no focus, in the look of Tk on X11.

The color of the highlight ring of -highlightthickness while the label does not have the focus.

% label .hb -highlightbackground white
> .hb cget -highlightbackground
white
% label .hb2 -highlightbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-highlightcolor COLOR

The color of the focus ring while the label has the focus, in the look of Tk on X11.

The color of the highlight ring of -highlightthickness while the label has the focus.

% label .hc -highlightcolor blue
> .hc cget -highlightcolor
blue
% label .hc2 -highlightcolor nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-highlightthickness DISTANCE

The width of the focus ring, in the look of Tk on X11.

The width of the ring around the label that shows the focus. The modern theme draws the focus with the stylesheet, so it stores the value with no effect on the size.

% label .ht -text Save
> label .ht5 -text Save -highlightthickness 5
> expr {[winfo reqwidth .ht5] == [winfo reqwidth .ht]}
1
% label .ht2 -highlightthickness x
bad screen distance "x"
-errorcode TK VALUE PIXELS

-image IMAGE

An image to show in place of the text.

The name of an image of image create. With an image and -compound none, the label shows the image and no text. The image must exist when the option is set.

label with -text Name -compound left and -image {}-image {}label with -text Name -compound left and -image ::ref::icon-image ::ref::icon
Each with -text Name -compound left. ::ref::icon is a photo image of 16 by 16 pixels.
# The text does not show, so the two labels have the same width.
% image create photo icon -width 40 -height 20
> label .im -image icon -text Save
> label .im2 -image icon
> expr {[winfo reqwidth .im] == [winfo reqwidth .im2]}
1
% label .im3 -image noimage
image "noimage" doesn't exist
-errorcode TK LOOKUP IMAGE noimage

-justify SIDE

The alignment of the lines of a text of more than one line.

How the lines of a text with more than one line line up with each other. A text breaks into lines at each newline and at -wraplength.

Values: left, center, right.

label with -text Short
A longer line and -justify left-justify leftlabel with -text Short
A longer line and -justify center-justify centerlabel with -text Short
A longer line and -justify right-justify right
Each with -text Short A longer line.
% label .ju -text "one\ntwo" -justify left
> .ju cget -justify
left
% label .ju2 -justify middle
bad justification "middle": must be left, right, or center
-errorcode TCL LOOKUP INDEX justification middle

-padx DISTANCE

The space at the left and the right of the content, in place of the padding of the theme.

The default 0 leaves the padding to the theme, and a value of the script replaces the padding of the theme at both sides.

label with -text Name and -padx 1-padx 1label with -text Name and -padx 4-padx 4label with -text Name and -padx 16-padx 16
Each with -text Name.
% label .px -text Save
> label .px20 -text Save -padx 20
> expr {[winfo reqwidth .px20] > [winfo reqwidth .px]}
1
% label .px2 -padx x
bad screen distance "x"
-errorcode TK VALUE PIXELS

-pady DISTANCE

The space above and below the content, in place of the padding of the theme.

The default 0 leaves the padding to the theme, and a value of the script replaces the padding of the theme above and below.

label with -text Name and -pady 1-pady 1label with -text Name and -pady 2-pady 2label with -text Name and -pady 10-pady 10
Each with -text Name.
% label .py -text Save
> label .py10 -text Save -pady 10
> expr {[winfo reqheight .py10] > [winfo reqheight .py]}
1
% label .py2 -pady x
bad screen distance "x"
-errorcode TK VALUE PIXELS

-relief RELIEF

The 3-D look of the border.

The style of the border: raised and sunken look 3-D, and groove and ridge are lines. The theme draws the border, and the CSS of the theme can give a relief its own look with border-style.

Values: flat, groove, raised, ridge, solid, sunken.

label with -text Name -borderwidth 4 and -relief flat-relief flatlabel with -text Name -borderwidth 4 and -relief raised-relief raisedlabel with -text Name -borderwidth 4 and -relief sunken-relief sunkenlabel with -text Name -borderwidth 4 and -relief groove-relief groovelabel with -text Name -borderwidth 4 and -relief ridge-relief ridgelabel with -text Name -borderwidth 4 and -relief solid-relief solid
Each with -text Name -borderwidth 4.
% label .re -text Go -relief groove
> .re cget -relief
groove
% label .re2 -relief bumpy
bad relief "bumpy": must be flat, groove, raised, ridge, solid, or sunken
-errorcode TCL LOOKUP INDEX relief bumpy

-state STATE

The look of the label: normal, active, or disabled.

The state changes only the colors of the label: active uses -activebackground and -activeforeground, and disabled uses -disabledforeground for the text. A label runs no command in any state.

ValueDescription
normalThe colors of -background and -foreground.
activeThe colors of -activebackground and -activeforeground.
disabledThe text color of -disabledforeground.
label with -text Name and -state normal-state normallabel with -text Name and -state disabled-state disabled
Each with -text Name.
% label .st -text Off -state disabled
> .st cget -state
disabled
% label .st2 -state busy
bad state "busy": must be active, disabled, or normal
-errorcode TCL LOOKUP INDEX state busy

-takefocus VALUE

Whether the Tab key can move the focus to the label.

1 puts the label in the order of the Tab key, and 0, the default of a label, leaves it out. An empty value uses the default rule of Tk for the widget. In Tk the value can also be a script, and its result is the answer. ztk stores each value with no check.

% label .tf -takefocus 1
> .tf cget -takefocus
1

ztk stores each value of the option with no check.

-text TEXT

The text of the label.

The text that the label shows. A newline starts a new line. A longer text makes a wider label, unless -width sets the width. With -textvariable, the variable sets the text.

label with -text Name-text Namelabel with -text {Name and more}-text {Name and more}
% label .tx -text Save
> set w [winfo reqwidth .tx]
> .tx configure -text "Save as"
> expr {[winfo reqwidth .tx] > $w}
1

Each string is a valid text.

-textvariable VAR

A global variable whose value is the text of the label.

The label shows the value of the global variable VAR, and changes its text when the variable changes. When VAR has no value, the label sets it to its -text. The variable stays after the label is destroyed.

% label .tv -textvariable label
> set label Open
> .tv cget -text
Open

% label .tv2 -textvariable start -text Start
> set start
Start

Each name is a valid variable name.

-underline INDEX

The index of a character of the text to underline, for a keyboard shortcut.

The index from 0 of the character of -text to underline. -1 underlines no character. The underline is a hint for a shortcut key, and the script binds the key itself.

% label .un -text Save -underline 0
> .un cget -underline
0
% label .un2 -underline x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-width CHARS

The width of the label in characters of the font, or in pixels for an image.

For a label with text, the width in average characters of the font. For a label with an image, the width in pixels. 0 makes the label as wide as its content.

label with -text Name and -width 0-width 0label with -text Name and -width 8-width 8label with -text Name and -width 14-width 14
Each with -text Name.
% label .wi -text Hi
> label .wi10 -text Hi -width 10
> expr {[winfo reqwidth .wi10] > [winfo reqwidth .wi]}
1
% label .wi2 -width x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-wraplength DISTANCE

The longest line of the text before it breaks into a new line.

A text longer than this distance breaks at a space into more lines. 0 breaks no line, so the text is one line long unless it has newlines.

label with -text A text that is long enough to wrap and -wraplength 0-wraplength 0label with -text A text that is long enough to wrap and -wraplength 60-wraplength 60
Each with -text A text that is long enough to wrap.
% label .wr -text "one two three four five six"
> label .wr60 -text "one two three four five six" -wraplength 60
> expr {[winfo reqheight .wr60] > [winfo reqheight .wr] && [winfo reqwidth .wr60] < [winfo reqwidth .wr]}
1
% label .wr2 -wraplength x
bad screen distance "x"
-errorcode TK VALUE PIXELS

Returns

PATHNAME.

Examples

% label .name -text Name
> .name cget -text
Name

% winfo class [label .go -text Go]
Label

Errors

# A label has no command.
% label .nocmd -text Hi -command x
unknown option "-command"
-errorcode TK LOOKUP OPTION -command

% label .x -text a -foo b
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo

% label .twice
> label .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS

# The parent .no does not exist.
% label .no.b
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no

% label
wrong # args: should be "label pathName ?-option value ...?"
-errorcode TCL WRONGARGS

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1

See also