ttk::label
Guide: Themed widgets (ttk)
Makes a themed label: a widget that shows a text, an image, or both, and runs no script.
ttk::label PATHNAME ?OPTION VALUE ...?
Description
Makes a themed label 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. The class of the label is TLabel, the name of its style. In ztk the label uses the code and the CSS rules of the classic label, so one stylesheet styles both.
A themed label has a few options for its look, such as -background, -foreground, and -font. An empty value of one of them uses the value of the style.
Widget commands
| Command | Description |
|---|---|
ttk::label state | Gives the state flags of the widget, or turns flags on and off. |
ttk::label instate | Tests the state flags of the widget, and runs a script when they match. |
ttk::label identify | Gives the name of the element of the widget at a point. |
ttk::label cget | Gives the value of an option of the widget. |
ttk::label configure | Gives or sets the options of the widget. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-anchor | anchor | w | anchor | The place of the content in a label larger than it. |
-background | color | empty | frameColor | The background color of the label, in place of the color of the style. |
-borderwidth | screen distance | empty | borderWidth | The width of the border, in place of the border of the style. |
-class | class name | empty | The class of the widget for the option database and for the bindings, set only at creation. | |
-compound | compound | empty | compound | Which of the text and the image show, and the place of the image. |
-cursor | cursor name | empty | cursor | The shape of the pointer over the widget. |
-font | font | empty | font | The font of the text, in place of the font of the style. |
-foreground | color | empty | textColor | The color of the text, in place of the color of the style. |
-image | image list | empty | image | An image to show, with other images for some states. |
-justify | justification | left | justify | The alignment of the lines of a text of more than one line. |
-padding | list of distances | empty | padding | The space between the border and the content, in place of the padding of the style. |
-relief | relief | empty | relief | The 3-D look of the border. |
-state | string | normal | state | A state for compatibility with the classic widgets: normal or disabled. |
-style | style name | empty | style | The style of the widget, such as TLabel or a style of the script such as Accent.TLabel. |
-takefocus | boolean, empty, or script | empty | takeFocus | Whether the Tab key can move the focus to the widget. |
-text | string | empty | text | The text of the widget. |
-textvariable | variable name | empty | textVariable | A global variable whose value is the text of the widget. |
-underline | integer | -1 | underline | The index of a character of the text to underline, for a keyboard shortcut. |
-width | integer | empty | width | The width of the text area in average characters of the font. |
-wraplength | screen distance | empty | wrapLength | The longest line of the text before it breaks into a new line. |
-anchor ANCHOR
The place of the content 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 -relief solid -borderwidth 1.% ttk::label .tlan -text Go -anchor e
> .tlan cget -anchor
e% ttk::label .tlan2 -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, in place of the color of the style.
The color behind the text. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB. An empty value uses the color of the style.

-background {}
-background {#fde68a}
-background {#bfdbfe}-text Name.% ttk::label .tlbg -background red
> .tlbg cget -background
red% ttk::label .tlbg2 -background nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-borderwidth DISTANCE
The width of the border, in place of the border of the style.
The width of the border around the label. An empty value uses the border of the style. 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 3-text Name -relief solid.% ttk::label .tlbw -text Hi
> ttk::label .tlbw5 -text Hi -borderwidth 5
> expr {[winfo reqwidth .tlbw5] - [winfo reqwidth .tlbw]}
10% ttk::label .tlbw2 -borderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-class CLASS
The class of the widget for the option database and for the bindings, set only at creation.
The class name replaces the class of the style, such as TLabel, in winfo class and in the bind tags of the widget. Give it in the call that makes the widget. A later configure of the option gives an error.
% ttk::label .tlcl -class Fancy
> winfo class .tlcl
Fancy
% ttk::label .tlcl3
> winfo class .tlcl3
TLabel% ttk::label .tlcl2
> .tlcl2 configure -class Other
attempt to change read-only option
-errorcode NONE-compound PLACE
Which of the text and the image show, and the place of the image.
An empty value or none shows the image when there is one, else the text. text shows only the text, and image shows only the image. center, top, bottom, left, and right show the two together, with the image at that side of the text, or under it with center.
Values: none, text, image, center, top, bottom, left, right.

-compound none
-compound text
-compound image
-compound center
-compound top
-compound bottom
-compound left
-compound right-text Name -image ::ref::icon. none shows the image when the widget has one, as image does, and as in Tk.% image create photo tlpic -width 40 -height 20
> ttk::label .tlcpi -image tlpic -text Save
> ttk::label .tlcpl -image tlpic -text Save -compound left
> expr {[winfo reqwidth .tlcpl] > [winfo reqwidth .tlcpi]}
1% ttk::label .tlco -compound sideways
bad compound "sideways": must be none, text, image, center, top, bottom, left, or right
-errorcode TCL LOOKUP INDEX compound sideways-cursor CURSOR
The shape of the pointer over the widget.
Each value side by side: the contact sheet of the cursors
A cursor name of Tk, such as hand2 or xterm. An empty value uses the cursor of the parent.
% ttk::label .tlcu -cursor hand2
> .tlcu cget -cursor
hand2ztk stores each value of the option with no check.
-font FONT
The font of the text, in place of the font of the style.
A font name, such as TkDefaultFont, or a font description, such as {Helvetica 12 bold}. An empty value uses the font of the style. A larger font makes a larger label.

-font TkDefaultFont
-font TkFixedFont
-font {Helvetica 16 bold}-text Name.% ttk::label .tlfo -text Hi
> ttk::label .tlfo2 -text Hi -font {Helvetica 24}
> expr {[winfo reqheight .tlfo2] > [winfo reqheight .tlfo]}
1% ttk::label .tlfo3 -font {Helvetica 12 weird}
unknown font style "weird"
-errorcode TK LOOKUP FONT_STYLE weird-foreground COLOR
The color of the text, in place of the color of the style.
The color of the text. An empty value uses the color of the style.

-foreground {}
-foreground {#b91c1c}
-foreground {#2563eb}-text Name.% ttk::label .tlfg -foreground #123456
> .tlfg cget -foreground
#123456% ttk::label .tlfg2 -foreground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-image IMAGESPEC
An image to show, with other images for some states.
The first word is the name of an image of image create. More words are pairs of a state spec and an image, such as {pic disabled pic2}. With an image and -compound none, the widget 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 widgets have the same width.
% image create photo tlicon -width 40 -height 20
> ttk::label .tlim -image tlicon -text Save
> ttk::label .tlim2 -image tlicon
> expr {[winfo reqwidth .tlim] == [winfo reqwidth .tlim2]}
1% ttk::label .tlim3 -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.% ttk::label .tlju -text "one\ntwo" -justify right
> .tlju cget -justify
right% ttk::label .tlju2 -justify middle
bad justification "middle": must be left, right, or center
-errorcode TCL LOOKUP INDEX justification middle-padding PADDING
The space between the border and the content, in place of the padding of the style.
A list of 1 to 4 distances: left, top, right, and bottom. One value is the space at each side. Two values are the left and right space and the top and bottom space. A missing third value is the first, and a missing fourth value is the second. An empty value uses the padding of the style.

-padding {}
-padding 4
-padding {16 4}-text Name -relief solid -borderwidth 1.% ttk::label .tlpa -text Hi
> ttk::label .tlpa30 -text Hi -padding 30
> expr {[winfo reqwidth .tlpa30] > [winfo reqwidth .tlpa]}
1ztk stores each value at configure, as Tk 8.6.16 does.
-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 border shows only with a -borderwidth above 0. An empty value uses the relief of the style.
Values: flat, groove, raised, ridge, solid, sunken.

-relief flat
-relief raised
-relief sunken
-relief groove
-relief ridge
-relief solid-text Name -borderwidth 4 -padding 4.% ttk::label .tlre -text Go -relief sunken -borderwidth 2
> .tlre cget -relief
sunken% ttk::label .tlre2 -relief bumpy
bad relief "bumpy": must be flat, groove, raised, ridge, solid, or sunken
-errorcode TCL LOOKUP INDEX relief bumpy-state STATE
A state for compatibility with the classic widgets: normal or disabled.
disabled sets the state flag disabled of the widget, and normal clears it. The state command changes the flags with no change of this option. ztk stores each other value with no check.

-state normal
-state disabled-text Name.% ttk::label .tlst -state disabled
> .tlst instate disabled
1
# The `state` command does not change the option.
% ttk::label .tlst2
> .tlst2 state disabled
> .tlst2 cget -state
normalztk stores each value of the option with no check, as Tk 8.6.16 does.
-style STYLE
The style of the widget, such as TLabel or a style of the script such as Accent.TLabel.
An empty value uses the style TLabel. A style name of the script ends with the name of the base style, such as Accent.TLabel, and ttk::style configure and ttk::style map give it its options. A style with no settings looks like its base style.
% ttk::style configure Accent.TLabel -foreground white
> ttk::label .tlsy -style Accent.TLabel
> .tlsy cget -style
Accent.TLabelztk stores each value of the option with no check, as Tk 8.6.16 does at creation.
-takefocus VALUE
Whether the Tab key can move the focus to the widget.
1 puts the widget in the order of the Tab key, and 0 leaves it out. The default empty value uses the default rule of Tk, and this widget is not in the order of the Tab key with it. A script runs with the path of the widget appended, and its result is the answer.
% ttk::label .tltf -takefocus 1
> .tltf cget -takefocus
1ztk stores each value of the option with no check.
-text TEXT
The text of the widget.
The text that the widget shows. A newline starts a new line. A longer text makes a wider widget, unless -width sets the width. With -textvariable, the variable sets the text.

-text Name
-text {Full name}% ttk::label .tltx -text Save
> set w [winfo reqwidth .tltx]
> .tltx configure -text "Save as"
> expr {[winfo reqwidth .tltx] > $w}
1Each string is a valid text.
-textvariable VAR
A global variable whose value is the text of the widget.
The widget shows the value of the global variable VAR in place of -text, and changes its text when the variable changes. The variable stays after the widget is destroyed.
% set tltitle Open
> ttk::label .tltv -textvariable tltitle
> set w [winfo reqwidth .tltv]
> set tltitle "Open a file"
> expr {[winfo reqwidth .tltv] > $w}
1Each 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.
% ttk::label .tlun -text Save -underline 0
> .tlun cget -underline
0% ttk::label .tlun2 -underline x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-width CHARS
The width of the text area in average characters of the font.
A positive value sets the width of the text area. A negative value sets a minimum width, and a longer text makes the widget wider. An empty value makes the widget as wide as its content.

-width {}
-width 8
-width 16-text Name -relief solid -borderwidth 1.% ttk::label .tlwi -text Hi
> ttk::label .tlwi10 -text Hi -width 10
> expr {[winfo reqwidth .tlwi10] > [winfo reqwidth .tlwi]}
1ztk stores each value at configure, as Tk 8.6.16 does.
-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. An empty value or 0 breaks no line, so the text is one line long unless it has newlines.

-wraplength {}
-wraplength 60
-wraplength 120-text A label with a long text that wraps.% ttk::label .tlwr -text "one two three four five six"
> ttk::label .tlwr60 -text "one two three four five six" -wraplength 60
> expr {[winfo reqheight .tlwr60] > [winfo reqheight .tlwr] && [winfo reqwidth .tlwr60] < [winfo reqwidth .tlwr]}
1% ttk::label .tlwr2 -wraplength x
bad screen distance "x"
-errorcode TK VALUE PIXELSStates
| State | Description |
|---|---|
disabled | The theme draws the text grayed. |

no state flag
state disabled-text Name.Returns
PATHNAME.
Examples
% ttk::label .name -text Name
> .name cget -text
Name
% winfo class [ttk::label .go -text Go]
TLabel
Errors
# A label has no command.
% ttk::label .nocmd -text Hi -command x
unknown option "-command"
-errorcode TK LOOKUP OPTION -command
% ttk::label .twice
> ttk::label .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% ttk::label .no.l
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% ttk::label
wrong # args: should be "ttk::label pathName ?-option value ...?"
-errorcode TCL WRONGARGS
Differences from Tcl
-cursoraccepts each value with no check. Tk 8.6.16 gives an error for a cursor name that does not exist.- The errors of
state,instate,identify, and of an unknown widget command have the error codeNONE. Tk 8.6.16 gives a code such asTTK VALUE STATEorTCL LOOKUP INDEX. The error of-underlinehas the codeTCL VALUE NUMBER, where Tk 8.6.16 givesTCL VALUE INTEGER. - The states
activeandhoverare one flag, so-state activeturns on both.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1