beetlebug▾
/

ttk::label

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

CommandDescription
ttk::label stateGives the state flags of the widget, or turns flags on and off.
ttk::label instateTests the state flags of the widget, and runs a script when they match.
ttk::label identifyGives the name of the element of the widget at a point.
ttk::label cgetGives the value of an option of the widget.
ttk::label configureGives or sets the options of the widget.

Options

OptionTypeDefaultDatabase nameDescription
-anchoranchorwanchorThe place of the content in a label larger than it.
-backgroundcoloremptyframeColorThe background color of the label, in place of the color of the style.
-borderwidthscreen distanceemptyborderWidthThe width of the border, in place of the border of the style.
-classclass nameemptyThe class of the widget for the option database and for the bindings, set only at creation.
-compoundcompoundemptycompoundWhich of the text and the image show, and the place of the image.
-cursorcursor nameemptycursorThe shape of the pointer over the widget.
-fontfontemptyfontThe font of the text, in place of the font of the style.
-foregroundcoloremptytextColorThe color of the text, in place of the color of the style.
-imageimage listemptyimageAn image to show, with other images for some states.
-justifyjustificationleftjustifyThe alignment of the lines of a text of more than one line.
-paddinglist of distancesemptypaddingThe space between the border and the content, in place of the padding of the style.
-reliefreliefemptyreliefThe 3-D look of the border.
-statestringnormalstateA state for compatibility with the classic widgets: normal or disabled.
-stylestyle nameemptystyleThe style of the widget, such as TLabel or a style of the script such as Accent.TLabel.
-takefocusboolean, empty, or scriptemptytakeFocusWhether the Tab key can move the focus to the widget.
-textstringemptytextThe text of the widget.
-textvariablevariable nameemptytextVariableA global variable whose value is the text of the widget.
-underlineinteger-1underlineThe index of a character of the text to underline, for a keyboard shortcut.
-widthintegeremptywidthThe width of the text area in average characters of the font.
-wraplengthscreen distanceemptywrapLengthThe 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.

ttk::label with -text Name -width 14 -relief solid -borderwidth 1 and -anchor w-anchor wttk::label with -text Name -width 14 -relief solid -borderwidth 1 and -anchor center-anchor centerttk::label with -text Name -width 14 -relief solid -borderwidth 1 and -anchor e-anchor e
Each with -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.

ttk::label with -text Name and -background {}-background {}ttk::label with -text Name and -background {#fde68a}-background {#fde68a}ttk::label with -text Name and -background {#bfdbfe}-background {#bfdbfe}
Each with -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.

ttk::label with -text Name -relief solid and -borderwidth 0-borderwidth 0ttk::label with -text Name -relief solid and -borderwidth 1-borderwidth 1ttk::label with -text Name -relief solid and -borderwidth 3-borderwidth 3
Each with -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.

ttk::label with -text Name -image ::ref::icon and -compound none-compound nonettk::label with -text Name -image ::ref::icon and -compound text-compound textttk::label with -text Name -image ::ref::icon and -compound image-compound imagettk::label with -text Name -image ::ref::icon and -compound center-compound centerttk::label with -text Name -image ::ref::icon and -compound top-compound topttk::label with -text Name -image ::ref::icon and -compound bottom-compound bottomttk::label with -text Name -image ::ref::icon and -compound left-compound leftttk::label with -text Name -image ::ref::icon and -compound right-compound right
Each with -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.

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
hand2

ztk 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.

ttk::label with -text Name and -font TkDefaultFont-font TkDefaultFontttk::label with -text Name and -font TkFixedFont-font TkFixedFontttk::label with -text Name and -font {Helvetica 16 bold}-font {Helvetica 16 bold}
Each with -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.

ttk::label with -text Name and -foreground {}-foreground {}ttk::label with -text Name and -foreground {#b91c1c}-foreground {#b91c1c}ttk::label with -text Name and -foreground {#2563eb}-foreground {#2563eb}
Each with -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.

ttk::label with -text Name -compound left and -image {}-image {}ttk::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 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.

ttk::label with -text Short
A longer line and -justify left-justify leftttk::label with -text Short
A longer line and -justify center-justify centerttk::label with -text Short
A longer line and -justify right-justify right
Each with -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.

ttk::label with -text Name -relief solid -borderwidth 1 and -padding {}-padding {}ttk::label with -text Name -relief solid -borderwidth 1 and -padding 4-padding 4ttk::label with -text Name -relief solid -borderwidth 1 and -padding {16 4}-padding {16 4}
Each with -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]}
1

ztk stores each value at configure, as Tk 8.6.16 does.

-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 border shows only with a -borderwidth above 0. An empty value uses the relief of the style.

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

ttk::label with -text Name -borderwidth 4 -padding 4 and -relief flat-relief flatttk::label with -text Name -borderwidth 4 -padding 4 and -relief raised-relief raisedttk::label with -text Name -borderwidth 4 -padding 4 and -relief sunken-relief sunkenttk::label with -text Name -borderwidth 4 -padding 4 and -relief groove-relief groovettk::label with -text Name -borderwidth 4 -padding 4 and -relief ridge-relief ridgettk::label with -text Name -borderwidth 4 -padding 4 and -relief solid-relief solid
Each with -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.

ttk::label with -text Name and -state normal-state normalttk::label with -text Name and -state disabled-state disabled
Each with -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
normal

ztk 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.TLabel

ztk 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
1

ztk 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.

ttk::label with -text Name-text Namettk::label with -text {Full name}-text {Full name}
% ttk::label .tltx -text Save
> set w [winfo reqwidth .tltx]
> .tltx configure -text "Save as"
> expr {[winfo reqwidth .tltx] > $w}
1

Each 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}
1

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.

% 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.

ttk::label with -text Name -relief solid -borderwidth 1 and -width {}-width {}ttk::label with -text Name -relief solid -borderwidth 1 and -width 8-width 8ttk::label with -text Name -relief solid -borderwidth 1 and -width 16-width 16
Each with -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]}
1

ztk 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.

ttk::label with -text A label with a long text that wraps and -wraplength {}-wraplength {}ttk::label with -text A label with a long text that wraps and -wraplength 60-wraplength 60ttk::label with -text A label with a long text that wraps and -wraplength 120-wraplength 120
Each with -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 PIXELS

States

StateDescription
disabledThe theme draws the text grayed.
ttk::label with -text Name and no state flagno state flagttk::label with -text Name and state disabledstate disabled
Each with -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

Facts

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

See also