Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Tkinter Label Widget

Python Tkinter Label Widget

Python GUI Programming Python GUI Programming

Text and images displayed in the window specified by the Python Tkinter Label widget.

Syntax

The syntax format is as follows:


w = Label ( master, option, ... )
No. Options & Description
1

anchor

The position of the text or image in the background content area, defaulting to center. Possible values are (n, s, w, e, ne, nw, sw, se, center). eswn are the first letters of east, south, west, north in English, indicating: north up, south down, west left, east right.

2

bg

The background color of the label

4

bd

The size of the label, defaulting to 2 pixels

4

bitmap

Specify the bitmap on the label; if an image is specified, this option is ignored

5

cursor

The shape of the cursor when the mouse moves over the label; can be set to arrow, circle, cross, plus, etc.

6

font

Set the font.

7

fg

Set the foreground color.

8

height

The height of the label, defaulting to 0.

9

image

Set the label image.

10

justify

Define the alignment; possible values are LEFT, RIGHT, CENTER, defaulting to CENTER.

11

padx

The x-axis spacing, in pixels, defaulting to 1.

12

pady

The y-axis spacing, in pixels, defaulting to 1.

13

relief

Border style, with options: FLAT, SUNKEN, RAISED, GROOVE, RIDGE. The default is FLAT.

14

text

Set the text, which can contain newlines (\n).

15

textvariable

The label displays a Tkinter variable, StringVar. If the variable is modified, the label text is updated automatically.

16

underline

Set the underline; the default is -1. If set to 1, the underline starts from the second character.

17

width

Set the label width, defaulting to 0, which means it is calculated automatically, in pixels.

10

wraplength

Set how many lines the label text is displayed on, defaulting to 0.

Example

#!/usr/bin/python

-- coding: UTF-8 --

Use tkinter in python3

import

tkinter

as

tk

Use Tkinter in python2

#import Tkinter as tk

root

=

tk

.

Tk

(

)

w

=

tk

.

Label

(

root

,

text

=

"

Hello RUNOOB!

"

)

w

.

pack

(

)

root

.

mainloop

(

)

The test output is as follows:

Python GUI Programming Python GUI Programming