label
Guide: Frames, labels, and buttons
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
| Command | Description |
|---|---|
label cget | Gives the value of an option of the label. |
label configure | Gives or sets the options of the label. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-activebackground | color | #ececec | activeBackground | The background color of the active state, in the look of Tk on X11. |
-activeforeground | color | #000000 | activeForeground | The text color of the active state, in the look of Tk on X11. |
-anchor | anchor | center | anchor | The place of the text or the image in a label larger than it. |
-background | color | #d9d9d9 | background | The background color of the label. |
-bitmap | bitmap name | empty | bitmap | A bitmap name of Tk to show in place of the text. |
-borderwidth | screen distance | 0 | borderWidth | The width of the border, in place of the border of the theme. |
-compound | compound | none | compound | Shows the image and the text together, with the image at that side. |
-cursor | cursor name | empty | cursor | The shape of the pointer over the label. |
-disabledforeground | color | #a3a3a3 | disabledForeground | The text color while the state is disabled. |
-font | font | TkDefaultFont | font | The font of the text. |
-foreground | color | #000000 | foreground | The color of the text. |
-height | integer | 0 | height | The height of the label in lines of text, or in pixels for an image. |
-highlightbackground | color | #d9d9d9 | highlightBackground | The color of the focus ring while the label has no focus, in the look of Tk on X11. |
-highlightcolor | color | #000000 | highlightColor | The color of the focus ring while the label has the focus, in the look of Tk on X11. |
-highlightthickness | screen distance | 0 | highlightThickness | The width of the focus ring, in the look of Tk on X11. |
-image | image name | empty | image | An image to show in place of the text. |
-justify | justification | center | justify | The alignment of the lines of a text of more than one line. |
-padx | screen distance | 0 | padX | The space at the left and the right of the content, in place of the padding of the theme. |
-pady | screen distance | 0 | padY | The space above and below the content, in place of the padding of the theme. |
-relief | relief | flat | relief | The 3-D look of the border. |
-state | state | normal | state | The look of the label: normal, active, or disabled. |
-takefocus | boolean, empty, or script | 0 | takeFocus | Whether the Tab key can move the focus to the label. |
-text | string | empty | text | The text of the label. |
-textvariable | variable name | empty | textVariable | A global variable whose value is the text of the label. |
-underline | integer | -1 | underline | The index of a character of the text to underline, for a keyboard shortcut. |
-width | integer | 0 | width | The width of the label in characters of the font, or in pixels for an image. |
-wraplength | screen distance | 0 | wrapLength | The 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.

-anchor w
-anchor center
-anchor e-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.

-background {#fde68a}
-background {#bfdbfe}-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.

-borderwidth 0
-borderwidth 1
-borderwidth 4-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.

-compound none
-compound center
-compound top
-compound bottom
-compound left
-compound right-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.
Each value side by side: the contact sheet of the cursors
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.

-disabledforeground {#888888}
-disabledforeground {#b91c1c}-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.

-font TkDefaultFont
-font TkFixedFont
-font {Helvetica 16 bold}-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.

-foreground {#b91c1c}
-foreground {#2563eb}-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.

-height 0
-height 2
-height 3-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.

-image {}
-image ::ref::icon-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.

-justify left
-justify center
-justify right-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.

-padx 1
-padx 4
-padx 16-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.

-pady 1
-pady 2
-pady 10-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.
Each value side by side: the contact sheet of the reliefs
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.

-relief flat
-relief raised
-relief sunken
-relief groove
-relief ridge
-relief solid-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.
| Value | Description |
|---|---|
normal | The colors of -background and -foreground. |
active | The colors of -activebackground and -activeforeground. |
disabled | The text color of -disabledforeground. |

-state normal
-state disabled-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
1ztk 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.

-text Name
-text {Name and more}% label .tx -text Save
> set w [winfo reqwidth .tx]
> .tx configure -text "Save as"
> expr {[winfo reqwidth .tx] > $w}
1Each 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
StartEach 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.

-width 0
-width 8
-width 14-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.

-wraplength 0
-wraplength 60-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 PIXELSReturns
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
- The size in a
-fontdescription can have a fraction, such as{Helvetica 12.5}. Tk 8.6.16 needs an integer. - The built-in bitmaps of
-bitmap, such asinfo, draw no pixel yet, and-underlinedraws no line (ztk#744).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1