Tkinter tutorial label

Source: Internet
Author: User
Tags tkinter tutorial

This article Reprinted from: http://blog.csdn.net/jcodeer/article/details/1811293

# Tkinter tutorial label
''' 1. The first example of label
Text attribute usage
'''
# To use the TK module, unless you do not want to use this module, you do not need to read the entire tutorial.
From tkinter import *
# Initialize TK
Root = TK ()
# Create a label and use the encoding. Up to now, no ide has been used directly through "drag-and-drop.
Label = label (root, text = 'Hello tkinter ')
# Display label, which must contain this statement
Label. Pack ()
# Root. Pack ()
# But root does not need it (strictly speaking, it must not be used in this way). Otherwise, the interpreter complains.
# Entering the message loop
Root. mainloop ()
# Control display steps:
#1. Create this control
#2. Specify the master of the space, that is, which control belongs
#3. Tell the GM (geometry manager) that a control has been generated
'''
There is also a simpler example: Print 'Hello tkinter 'to the title, and the label does not need to be created.
From tkinter import *
Root = TK ()
Root. Title ('Hello tkinter ')
Root. mainloop ()
You can't simplify it any more.
'''
''' 2. Use the built-in bitmap on the label
How to Use bitmap
'''
From tkinter import *
# Initialize TK
Root = TK ()
# Create a label and use the encoding. Up to now, no ide has been used directly through "drag-and-drop.
Label = label (root, bitmap = 'error ')
# The above Code uses the built-in bitmap Error

# Display label, which must contain this statement
Label. Pack ()

# Entering the message loop
Root. mainloop ()
'''
Other available bitmaps:
* Error
* Hourglass
* Info
* Questhead
* Question
* Warning
* Gray12
* Gray25
* Gray50
* Gray75
To view the effect, you can use the corresponding name to replace bitmpa = 'error.
It is said that you can also use a specified bitmap file. You can find it online in the following format:
Label (root, bitmap = "@/path/bitmapname ")
However, I tried it and never succeeded. I have changed the bitmap to a monochrome one :(

Other online articles show you how to use photoimage and bitmapimage to display BMP or GIF files.
To prevent image files from being automatically recycled by Python (garbage collected), put BMP or GIF to a global or entity
(Instance), the use of the following two methods, has not worked:
'''
# Using image attributes
# Bm = photoimage (file = 'C:/python.gif ')
# Label = label (root, image = BM)
# Label. BM = BM
# Error message:
# Tclerror: Image "pyimagexx" doesn' t exist
# Using bitmap attributes
# Bm = bitmapimage (file = 'C:/python2.bmp ')
# Label = label (root, bitmap = BM)
# Label. BM = BM
# Label. Pack ()
# Error message:
# Tclerror: Format error in bitmap data
'''
Although neither of them works, we still need to describe the relationship between bitmap and image. If both parameters are specified
Priority.
'''
''' 3. Change the foreground color and background color of the control.
FG: foreground color
BG: Background Color
A big use of setting the background color is: you can determine the size of the control (different controls use different colors, subsequent content
You can use this feature to debug container)
'''
From tkinter import *
Root = TK ()
# Specify the colors used when creating a label
''' Available color values :'''
# Use color name
Label (root, fg = 'red', BG = 'blue', text = 'Hello I am tkinter '). Pack ()
# Use color value # rrggbb
Label (root, fg = 'red', BG = '# ff00ff', text = 'Hello I am tkinter '). Pack ()
# Use System-related color values (Windows). We do not recommend using such values, which is not conducive to platform transplantation.
Label (root, fg = 'red', BG = 'systembuttonshadow', text = 'Hello I am tkinter '). Pack ()
Root. mainloop ()
'''
(1). Use the color name
Red
Green
Blue
Yellow
Lightblue
......
(2). Use # rrggbb
Label = label (root, fg = 'red', BG = '# ff00ff', text = 'Hello I am tkinter ')
Specifies that the background color is red.
(3). In addition, TK also supports OS-related color values, such as Windows.
Systemactiveborder,
Systemactivecaption,
Systemappworkspace,
Systembackground,
......
'''
'''4. Set the width and height
Width: width
Height: Height
'''
From tkinter import *
Root = TK ()
# Create three labels to display red, blue, and Yellow Labels respectively.
# Note the size of the three labels, which are related to the length of the text
Label (root, text = 'red', BG = 'red'). Pack ()
Label (root, text = 'blue', BG = 'blue'). Pack ()
Label (root, text = 'yellow', BG = 'yellow'). Pack ()

# Create three more labels. different from the previous one, the three labels both use the width and heigth attributes.
# The size of the three labels is specified by width and height.
Label (root, BG = 'red', width = 10, Height = 3). Pack ()
Label (root, BG = 'blue', width = 10, Height = 3). Pack ()
Label (root, BG = 'yellow', width = 10, Height = 3). Pack ()
Root. mainloop ()
'''5. Use both images and text
Compound: Specifies how text and bitmap/image are displayed on the label. The default value is none,
When image/bitmap is specified, text is overwritten and only the image is displayed. Available values:
Left: The image is left.
Right: Right of the image
Top: Top Images
Bottom: Image bottom
Center: Text on Images
Bitmap/image:
Image displayed on Label
Text:
Text displayed on the label
Label = label (root, text = 'error', compound = 'left', bitmap = 'error ')
'''
From tkinter import *
Root = TK ()
# Demonstrate how to use compound
# Position of image and text in label
# Image bottom
Label (root, text = 'botton ', compound = 'bottom', bitmap = 'error'). Pack ()
# Uploading images
Label (root, text = 'top', compound = 'top', bitmap = 'error'). Pack ()
# Right-side
Label (root, text = 'right', compound = 'right', bitmap = 'error'). Pack ()
# Image left
Label (root, text = 'left', compound = 'left', bitmap = 'error'). Pack ()
# Text on Images
Label (root, text = 'center', compound = 'center', bitmap = 'error'). Pack ()

# Message loop
Root. mainloop ()

'''6. Multi-line display of text
In tk004, width and heigth are used to specify the control size. If the specified size cannot meet the text requirements
What is it? The following code:
Label (root, BG = 'Welcome to jcodeer.cublog.cn ', width = 10, Height = 3). Pack ()
Run the program. The text that exceeds the label is truncated. The common method is to use the automatic line feed function, and when the text length is greater
When the width of the control, the text should be changed to the next line. TK will not be automatically processed, but the following attributes are provided:
Wraplength: specifies the number of units before starting line feed.
Justify: Specifies the alignment of multiple rows.
Ahchor: Specifies the position where the text or image (Bitmap/image) is displayed in the label.
Available values:
E
W
N
S
Ne
Se
SW
Sn
Center
Layout as shown in Figure

NW n ne
W center E
SW s se
'''
From tkinter import *
Root = TK ()
# Align left and center text
Label (root, text = 'Welcome to jcodeer.cublog.cn ', BG = 'yellow', width = 40, Height = 3, wraplength = 80, justify = 'left'). Pack ()
# Center alignment, text left
Label (root, text = 'Welcome to jcodeer.cublog.cn ', BG = 'red', width = 40, Height = 3, wraplength = 80, anchor = 'W'). Pack ()
# Align the center and align the text to the right
Label (root, text = 'Welcome to jcodeer.cublog.cn ', BG = 'blue', width = 40, Height = 3, wraplength = 80, anchor = 'E'). Pack ()

Root. mainloop ()

'''
After running the program, we can intuitively see the difference between justify and anchor: one is used to control the alignment of multiple rows, and the other is used to control the alignment of multiple rows.
Controls the position of the entire text block in the label
'''

Tkinter tutorial label

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.