Patterns
Methods Options
The Label widget is a standard Tkinter widget used to display a text or image on the screen. The button can only display text in a single font, but the text may span more than one line. In addition, one of the characters can be underlined, for example to mark a keyboard shortcut.
When to use the Label Widget
Labels are used to display texts and images. The label widget uses double buffering, so you can update the contents at any time, without annoying flicker.
To display data that the user can manipulate in place, it's probably easier to use the Canvas widget.
Back Next
http://www.pythonware.com/library/tkinter/introduction/label.htm [3/29/2003 12:46:33 AM]
Patterns
pythonware.com ::: library ::: An Introduction to Tkinter
Back Next
Patterns
To use a label, you just have to specify what to display in it (this can be text, a bitmap, or an image):
w = Label(master, text="Hello, world!")
If you don't specify a size, the label is made just large enough to hold its contents. You can also use the height and width options to explicitly set the size. If you display text in the label, these options define the size of the label in text units. If you display bitmaps or images instead, they define the size in pixels (or other screen units). See the Button description for an example how to specify the size in pixels also for text labels.
You can specify which color to use for the label with the foreground (or fg) and background (or bg) options. You can also choose which font to use in the label (the following example uses Tk 8.0 font descriptors). Use colors and fonts sparingly; unless you have a good reason to do otherwise, you should stick to the default values.
w = Label(master, text="Rouge", fg="red")
w = Label(master, text="Helvetica", font=("Helvetica", 16)) Labels can display multiple lines of text. You can use newlines or use the wraplength option to make the label wrap text by itself. When wrapping text, you might wish to use the anchor and justify options to make things look exactly as you wish. An example:
w = Label(master, text=longtext, anchor=W, justify=LEFT) You can associate a variable with the label. When the contents of the variable changes, the label is automatically updated:
v = StringVar()
Label(master, textvariable=v).pack() v.set("New Text!")
Back Next
http://www.pythonware.com/library/tkinter/introduction/x5232-patterns.htm [3/29/2003 12:46:34 AM]
Methods
pythonware.com ::: library ::: An Introduction to Tkinter
Back Next
Methods
The Label widget supports the standard Tkinter Widget interface. There are no additional methods.
Back Next
http://www.pythonware.com/library/tkinter/introduction/x5254-methods.htm [3/29/2003 12:46:35 AM]
Options
pythonware.com ::: library ::: An Introduction to Tkinter
Back Next
Options
The following options can be used for the Label widget.
Table 30-1. Label Options
Option Type Description
text string The text to display in the label. The text can contain newlines. If the bitmap or image options are used, this option is ignored.
bitmap bitmap The bitmap to display in the widget. If the image option is given, this option is ignored.
The following bitmaps are available on all
platforms: "error", "gray75", "gray50", "gray25",
"gray12", "hourglass", "info", "questhead",
"question", and "warning".
The following additional bitmaps are available on the Macintosh only: "document", "stationery",
"edition", "application", "accessory", "folder",
"pfolder", "trash", "floppy", "ramdisk", "cdrom",
"preferences", "querydoc", "stop", "note", and
"caution".
You can also load the bitmap from an XBM file.
Just prefix the filename with an at-sign, for example "@sample.xbm".
http://www.pythonware.com/library/tkinter/introduction/x5258-options.htm (1 of 3) [3/29/2003 12:46:36 AM]
Options
image image The image to display in the widget. If specified, this takes precedence over the text and bitmap
options.
width, height
int The size of the label. If the label displays text, the size is given in text units. If the label displays an image, the size is given in pixels (or screen units). If the size is omitted, it is calculated based on the label contents.
relief constant Border decoration. The default is FLAT. Other possible values are SUNKEN, RAISED, GROOVE, and RIDGE.
Note that to show the border, you need to change the borderwidth from it's default value of 0.
borderwidth (bd)
dimension The width of the label border. The default is 0 (no border).
background (bg),
foreground (fg)
color The label color (the foreground value is used for text and bitmap labels only). The default is platform specific.
font font The font to use in the label. The label can only contain text in a single font.
justify constant Defines how to align multiple lines of text. Use LEFT, RIGHT, or CENTER.
anchor constant Controls where in the label the text (or image) should be located. Use one of N, NE, E, SE, S, SW, W, NW, or CENTER. Default is CENTER.
http://www.pythonware.com/library/tkinter/introduction/x5258-options.htm (2 of 3) [3/29/2003 12:46:36 AM]
Options
wraplength distance Determines when a label's text should be wrapped into multiple lines. This is given in screen units.
Default is no wrapping.
textvariable variable Associates a Tkinter variable (usually a
StringVar) to the label. If the variable is changed, the label text is updated.
underline int Default is -1.
cursor cursor The cursor to show when the mouse is moved over the label.
Back Next
http://www.pythonware.com/library/tkinter/introduction/x5258-options.htm (3 of 3) [3/29/2003 12:46:36 AM]
The Listbox Widget
pythonware.com ::: library ::: An Introduction to Tkinter
Back Next
Chapter 31. The Listbox Widget
Table of Contents