02-tkinter Label Study Notes

Label Label

The Label () is used to create a text or an image in the window tag.
Usage: Label (parent, options, ......).

Commonly used options parameter:

(1) anthor: control the position of the label, the default is CENTER (centered).

(2) bg: background color.

(3) bitmap: use the default icon as the label content.

(4) borderwidth: label border width.

(5) compound: You can set the label contains images and text, the positional relationship between each other.

(6) cursor: the mouse cursor shape on the label.

(7) fg: foreground color.

(8) font: can be set font, a font style and size.

(9) height: the height of the label, in characters.

(10) width: width of the label, in characters.

(11) image: the image is displayed as a label.

(12) justify: When there are multiple lines of text of the last line aligned solutions, preferably LEFT / RIGHT / CENTER (left / right / center), the default is centered.

(13) padx / pady: label text between the tag spacing, in pixels.

(14) relief: controlling the shape of the label, the default is relief = FLAT.

(15) text: label content, can be used "\ n" multi-line input control.

(16) textvariable: Settings tab objects are displayed in a variable manner.

(17) underline: setting the first few words are underlined, starting from 0, the default is -1, indicating no underlined.

(18) wraplength: How linefeed width to the rear.

06- The first label program

from tkinter import *
win=Tk()
label=Label(win,text="hello tkinter")
label.pack() #pack布局。
win.mainloop()

07- add color to the label

from tkinter import *
win=Tk()
win.title("添加颜色")
label=Label(win,text="hello tkinter",bg="green",fg="blue",width=15,height=3)
label.pack()
win.mainloop()

08- Settings tab position

from tkinter import *
win=Tk()
win.title("标签颜色")
label=Label(win,text="hello tkinter" ,bg="green",fg="blue",anchor="nw",height=3,width=15)
label.pack()
win.mainloop()

At this time, the output of the tag in the upper left, the lower right corner of the tag if you want to output the anchor = "se", left-justified output "w", right-justified output "e",
the alignment of the output "n", the output aligned with "s".

09- After setting the text label wrap 30 pixels.

from tkinter import *
win=Tk()
win.title("设置标签文字换行输出")
label=Label(win,text="life is short,you need python",bg="green",fg="blue",width=20,height=7,anchor="nw",wraplength=30)
label.pack()
win.mainloop()

After wraplength = 30 sets of 30-pixel word wrap.

font attributes, including the following:

(1) font family: can refer to the word in all glyphs.

(2) font size: pixels.

(3) weight: normal (normal), bold (bold)

(4) underline: True or False

(5) overstrike: True or False

(6) slant: italic or roman

10- italicized font set when the label used, the pixel size is 30, bold.

from tkinter import *
win=Tk()
win.geometry('300x400')
win.title("设置标签字体")
label=Label(win,text="这是一段文字",bg="green",fg="blue",width=20,heigh=7,font="宋体 30 bold")
label.pack()
win.mainloop()

11- tag using a tag set last line of the left output.

from tkinter import *
win=Tk()
win.geometry('300x200')
win.title("设置标签文字靠左输出")
label=Label(win,text=" life is short,you need python",width=20,height=8,bg="green",fg="blue",wraplength=30,justify="left")
label.pack()
win.mainloop()

12- bit display error in the tag position of FIG.

from tkinter import *
win=Tk()
win.title("改变位图")
label=Label(win,bitmap="error")
label.pack()
win.mainloop()

In addition to the error value bitmap, as well as warning, hourglass, info, gray12, gray25, gray50, gray75, question, questhead like.

compound Parameters: When text and images coexist can use this parameter to define the relationship between text and image.
13- When the image and text coexist, the image on the left, text on the right.

from tkinter import *
win=Tk()
label=Label(win,text="这是测试文字",bitmap="error",compound="left")
label.pack()
win.mainloop()

In addition to left, as well as top (image at top), right (image on the right), center (text overlays on the image top), bottom (image below)

14- provided a raised tag attributes.

from tkinter import *
win=Tk()
label=Label(win,text="外边框设置",relief="raised")
label.pack()
win.mainloop()

In addition to raised relief properties, there are flat, groove, ridge, solid, sunken.

15 set the distance text and labels.

from tkinter import *
win=Tk()
label=Label(win,text="raised",relief="raised",bg="green",padx=5,pady=10)
label.pack()
win.mainloop()

At this time, the distance between the left and right label text is provided with a label 5, the vertical distance is 10.

16- Settings tab to display the picture.

from tkinter import *
win=Tk()
image_gif=PhotoImage(file=r"C:\Users\Administrator\Desktop\logo.gif")
label=Label(win,image=image_gif)
label.pack()
win.mainloop()

PhotoImage () method only supports .gif types of pictures, if you want to display .jpg type of image, you can use the PIL module Image and ImageTk.
17- display .jpg images.

from tkinter import *
from PIL import Image,ImageTk
win=Tk()
image=Image.open("logo.jpg")
t=ImageTk.PhotoImage(image)
label=Label(win,image=t)
label.pack()
win.mainloop()

If the program in the future want to add new properties or change existing property, you can use config () method.

18 design a counter, the counter content will be updated once per second.

from tkinter import *
counter=0
def run_count(digit):
def count():
global counter
counter=counter+1
digit.config(text=str(counter))
digit.after(1000,count)
count()

win=Tk()
digit=Label(win,bg="green",fg="blue",font="宋体 20 bold")
digit.pack()
run_count(digit)
win.mainloop()

Cursors property: when the cursor passes over the tag changes shape.

19-Cursors property of the application.

from tkinter import *
win=Tk()
label=Label(win,text="raised",relief="raised",bg="green",fg="black",padx=6,pady=10,cursor="clock")
label.pack()
win.mainloop()

Dividing line Separator:
20-1200 dividing line setting.

from tkinter import *
from tkinter.ttk import Separator
win=Tk()
label1=Label(win,text="这是标题",font="宋体 20 bold")
label1.pack()
sep=Separator(win,orient=HORIZONTAL)
sep.pack(fill=X,padx=5)
label2=Label(win,text="这是内容",font="宋体 20 bold")
label2.pack()
win.mainloop()

fill = X indicates the dividing line to fill the X-axis, padx = about 5 represents a dividing line 5 and the window average pixel distance, orient = HORIZONTAL represents horizontal dividing line, when orient = VERTICAL the vertical dividing line is provided.

Guess you like

Origin blog.51cto.com/13526792/2447253