Python GUI----Label

Source: Internet
Author: User

A label component has been used in the previous article.

The steps for using the components are as follows:

1. Create this control


2. Specify the master of this space, that is, which one of the controls belongs to


3. Tell GM (geometry manager) that a control has generated a


How to use 1.text properties
Import Tkinter                                   top=tkinter. Tk ()                               label=tkinter. Label (top,text= "Hello World")       Label.pack ()                                  
How to use 2.label built-in bitmap bitmap
Import tkinter                                   top = tkinter. Tk () label = Tkinter. Label (top,bitmap = ' error ')   #上面的代码使用了内置位图errorlabel. Pack () Top.mainloop ()


3. Change the foreground and background color of the control ' FG: foreground color
BG: Background Color
One big use for setting the background color is that you can determine the size of the control (different controls use different colors, and subsequent content can use this attribute to debug container)

From tkinter Import *root = Tk () #在创建Label时指定各自使用的颜色 "' can use color values: ' ' #使用颜色名称Label (root,fg = ' red ', bg = ' Blue ', Text = ' Hello I Am Tkinter '). Pack () #使用颜色值 #rrggbblabel (root,fg = ' red ', bg = ' #FF00FF ', Text = ' Hello I am Tkinter '). Pack () #使用系统相关的颜色值 (wind OWS), it is not recommended to use such a value, which is not conducive to platform porting label (ROOT,FG = ' red ', bg = ' Systembuttonshadow ', Text = ' Hello I am Tkinter '). Pack () Root.mainloop ( )

4. Set width and height
From tkinter Import *root = Tk () #创建三个Label, showing red,blue,yellow# note the size of the three labels, which are all 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 () #再创建三个Label, Unlike the last one, these three labels use the width and Heigth property # Three label size to specify the label by width and height (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 the image and the text compound: Specifies how the text and the image (Bitmap/image) are displayed on the label, default to None, and when the IMAGE/BITMAP is specified, the text is overwritten and only the image is displayed. The values that can be used:
Left: Image
Right: the image is
Top: Image on top
Bottom: Image under
Center: Text Overlay on image
Bitmap/image: The image displayed on the label
Text: A literal displayed on a label
Label = label (Root,text = ' ERROR ', compound = ' Left ', bitmap = ' error ')

From tkinter Import *root = Tk () #演示compound的使用方法 # Image with text in label position # Image under label (Root,text = ' botton ', compound = ' bottom ', Bitmap = ' error '). Pack () #图像居上Label (root,text = ' top ', compound = ' top ', bitmap = ' error '). Pack () #图像居右Label (Root,text = ' Right ', compound = ' right ', bitmap = ' error '). Pack () #图像居左Label (Root,text = "Left", compound = ' Left ', bitmap = ' error '). Pack () #文字覆盖在图像上Label (root,text = ' center ', compound = ' center ', bitmap = ' error '). Pack () #消息循环root. Mainloop ()

6. Multiple lines of text are displayed in Tk004, using width and heigth to specify the size of the control, what happens if the specified size does not meet the requirements of the text? The following code:
Label (ROOT,BG = ' Welcome to jcodeer.cublog.cn ', width = 10,height = 3). Pack ()
Run the program, the part of the text that goes beyond the label is truncated, and the usual method is to use the Wrap feature, and when the text length is greater than the width of the control, the text should be displayed on the next line, and TK will not be processed automatically, but it provides properties:
Wraplength: Specify how many units to start line wrapping
Justify: Specifies the alignment of multiple lines
Ahchor: Specifies where the text or image (Bitmap/image) appears in the label
Available values:
E
W
N
S
Ne
Se
Sw
sn
Center
Layouts such as


NW N NE
W Center E
SW s SE
From tkinter Import *root = Tk () #左对齐, text centered label (Root,text = ' Welcome to jcodeer.cublog.cn ', bg = ' yellow ', width = 40,height = 3,wraplength = 80,justify = ' left '). Pack () #居中对齐, the text is the label (Root,text = ' Welcome to jcodeer.cublog.cn ', bg = ' red ', width = 40,height = 3,wraplength = 80,anchor = ' W '). Pack () #居中对齐, Text right label (Root,text = ' Welcome to jcodeer.cublog.cn ', bg = ' Blue ', W Idth = 40,height = 3,wraplength = 80,anchor = ' e '). Pack () Root.mainloop ()













If you have any questions, welcome to my public question ~

Python GUI----Label

Related Article

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.