Tkinter.Label label

Label label is mainly used to display text information, but also can display images. The syntax format is as follows: Label (parent object, options,...)

Summary of common attributes

Insert picture description here
Insert picture description here
The above picture is from the rookie tutorial, direct to: https://www.runoob.com/python/python-tk-label.html

Simply create a window with a label

Simple window creation

from tkinter import *
root=Tk()
root.title('萤火虫')
root.geometry('300x400+200+200')
root.configure(bg='pink')
root.iconbitmap('1.ico')
label=Label(root,text="hello tkinter!")
label.pack()
root.mainloop()

Operating results:
Insert picture description here
object-oriented writing

from tkinter import *
class Application(Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.master=master
        self.pack()
        self.createWidget()

    def createWidget(self):
        #创建label组件
        self.lable=Label(self,text="hello tkinter!")
        self.lable.pack()
if __name__ == '__main__':
    root=Tk()
    root.geometry('300x400+200+200')
    root.title('萤火虫')
    root.configure(bg='pink')
    root.iconbitmap('1.ico')
    app=Application(master=root)
    root.mainloop()

The effect of these two implementations is the same.

Set the label attribute

1. Purpose: Set the text foreground color to red and the background color to white.

self.lable=Label(self,text="hello tkinter!",
                         fg="red",bg="white")

Running results:
Insert picture description here
2. Purpose: Specify the label label width as 10 and height as 4.

self.lable=Label(self,text="hello tkinter!",
                         fg="red",bg="white",
                         width=20,height=3)

Operation result:
Insert picture description here
3. Purpose: Specify the position of the label text in the label, nw.
Note:Insert picture description here

self.lable=Label(self,text="hello tkinter!",
                         fg="red",bg="white",
                         width=20,height=3,
                         anchor="nw")

You can also use uppercase constants, NW N NE W CENTER E SW S SE
without quotation marks
. 4. Purpose: display the text in a new line. Make the text in the label wrap automatically when it reaches 50 pixels.

self.lable=Label(self,text="hello tkinter!",
                         fg="red",bg="white",
                         width=20,height=3,
                         anchor="nw",
                         wraplength=50)

Insert picture description here
5. Purpose: Set the font to Kai body, 20 pixels, with underline.
Note: 1.family font 2.size font size 3.weight bold hold, normal 4.underline 5.overstrike

self.lable=Label(self,text="hello tkinter!",
                         fg="red",bg="white",
                         width=20,height=3,
                         anchor="nw",
                         font=("kaiti",15,"underline"))

Insert picture description here
6. Purpose: Specify multi-line output qwertyuiopasdfghjklzxcvbnm, and set the last line to be aligned to the left.
The justify parameter of label can set the output of the last line of content to be left to the left, right to the right, and center to the center by default.
Insert picture description here
Set the justify parameter.

self.lable=Label(self,text="qwertyuiopasdfghjklzxcvbnm",
                         fg="red",bg="white",
                         width=20,height=3,
                         wraplength=80,
                         justify="left"
                         )

Insert picture description here
7. Use the relief property to establish a border.
Attribute value effect: flat groove raised ridge solid sunken

self.lable=Label(self,text="萤火虫",
                      relief="sunken"
                         )

Insert picture description here
8. Change the distance between the label text and the label interval
. The effect when padx/pady is not set.
Insert picture description here
After setting

self.lable=Label(self,text="萤火虫",
                      padx=10,pady=10
                         )

Insert picture description here
9. Display image

#创建label组件
self.image=PhotoImage(file="1.png")
self.lable=Label(self,image=self.image)

Insert picture description here
This method can only be applied to png and GIF images.
If you want to display jpg pictures, you need to install the pillow module
from PIL import Image,ImageTk

self.image_file=Image.open("3.jpg")
self.image=ImageTk.PhotoImage(self.image_file)
self.lable=Label(self,image=self.image)

Insert picture description here
The text and the picture appear at the same time.
Compound: The left image is on the left, the right image is on the right, the top image is on the top, the bottom image is on the bottom, and the center text is overlaid on the top of the image.

self.image=PhotoImage(file="1.png")
self.lable=Label(self,text="萤火虫寄语",
                      image=self.image,
                      compound="center")

Insert picture description here
10. Change the cursor shape. cursor
Insert picture description here
Note: The picture comes from the Internet.

Guess you like

Origin blog.csdn.net/qq_44862120/article/details/107914917