Tag Label
Label() is used to create a text or image label in the window.
Usage: label (parent object, options,...).
Common options parameters:
(1) anchor: controls the label position. The default is CENTER.
(2) bg: background color.
(3) bitmap: use the default icon as the label content.
(4) border width: the border width of the label.
(5) compound: you can set the position relationship between the labels containing pictures and text.
(6) cursor: the shape of the mouse cursor resting on the label.
(7) fg: foreground color.
(8) font: font, font style and size can be set.
(9) height: label height, unit is character.
(10) width: label width, unit: character.
(11) image: the label is displayed as an image.
(12) justify: when there are multiple lines of text, the alignment scheme of the last line can be left / right / Center (left / right / Center). The default is center alignment.
(13) padx/pady: the space between label text and label, in pixels.
(14) relief: control the shape of the label. The default value is relief=FLAT.
(15) text: label content. You can use "\ n" to control multi line input.
(16) textvariable: set the label object to display in variable mode.
(17) underline: set the underline of the first text, starting from 0. The default value is - 1, indicating no underline.
(18) wrap length: wrap lines after the width.
06 - first labeling procedure
from tkinter import * win=Tk() label=Label(win,text="hello tkinter") label.pack() #pack layout. win.mainloop()
07 - add color to label
from tkinter import * win=Tk() win.title("Add color") label=Label(win,text="hello tkinter",bg="green",fg="blue",width=15,height=3) label.pack() win.mainloop()
08 - set label position
from tkinter import * win=Tk() win.title("Label color") label=Label(win,text="hello tkinter" ,bg="green",fg="blue",anchor="nw",height=3,width=15) label.pack() win.mainloop()
At this time, the label is output at the top left. If you want the label to be output at the bottom right corner, anchor="se", w for left aligned output and e for right aligned output,
Use "n" for top aligned output and "s" for bottom aligned output.
09 - set the text in the label to 30 pixels before wrapping.
from tkinter import * win=Tk() win.title("Set label text wrapping output") 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()
Wrap length = 30 will set the text to wrap automatically when it reaches 30 pixels.
font attribute, mainly including the following:
(1) font family: you can refer to all fonts in word.
(2) size: the unit is pixel.
(3) weight: normal, bold
(4) underline: True or False
(5) overstrike: True or False
(6) slant:italic or roman
10 - set the Song typeface when using the label, with the size of 30 pixels and bold display.
from tkinter import * win=Tk() win.geometry('300x400') win.title("Set label font") label=Label(win,text="This is a passage",bg="green",fg="blue",width=20,heigh=7,font="Song style 30 bold") label.pack() win.mainloop()
11 - use the label to set the left output of the last line of the label.
from tkinter import * win=Tk() win.geometry('300x200') win.title("Set label text left output") 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 - display error bitmap at label position
from tkinter import * win=Tk() win.title("Change bitmap") label=Label(win,bitmap="error") label.pack() win.mainloop()
In addition to error values, bitmap also includes warning, hourglass, info, gray12, gray25, gray50, gray75, question, and questionhead.
compound parameter: when text and image coexist, you can use this parameter to define the relationship between text and image.
13 - when image and text coexist, image is on the left and text is on the right.
from tkinter import * win=Tk() label=Label(win,text="This is the test text",bitmap="error",compound="left") label.pack() win.mainloop()
In addition to left, there are top, right, center, bottom
14 - set a label for the raised property.
from tkinter import * win=Tk() label=Label(win,text="Outer border settings",relief="raised") label.pack() win.mainloop()
Besides the raised attribute, relief also includes flat, groove, ridge, solid and sunken.
15 - set the distance between the text and the label.
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, set the left and right distance between the label text and the label as 5, and the upper and lower distance as 10.
16 - set the label 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()
The PhotoImage() method only supports. gif type pictures. If you want to display. jpg type pictures, you can use the Image and ImageTk in the PIL module.
17 display. jpg picture.
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 your program wants to add new properties or change existing properties in the future, you can use the config() method.
18 - design a counter and update its contents once a 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="Song style 20 bold") digit.pack() run_count(digit) win.mainloop()
Cursors property: the cursor changes shape as it passes over the label.
19 cursors attribute 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()
Divider:
20 - set the split line.
from tkinter import * from tkinter.ttk import Separator win=Tk() label1=Label(win,text="This is the title.",font="Song style 20 bold") label1.pack() sep=Separator(win,orient=HORIZONTAL) sep.pack(fill=X,padx=5) label2=Label(win,text="This is the content.",font="Song style 20 bold") label2.pack() win.mainloop()
fill=X means that the split line fills the X-axis, padx=5 means that the split line is 5 pixels away from the left and right of the window, original = horizontal means that the horizontal split line is set, and when original = vertical means that the vertical split line is set.