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
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:
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:
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:
3. Purpose: Specify the position of the label text in the label, nw.
Note:
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)
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"))
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.
Set the justify parameter.
self.lable=Label(self,text="qwertyuiopasdfghjklzxcvbnm",
fg="red",bg="white",
width=20,height=3,
wraplength=80,
justify="left"
)
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"
)
8. Change the distance between the label text and the label interval
. The effect when padx/pady is not set.
After setting
self.lable=Label(self,text="萤火虫",
padx=10,pady=10
)
9. Display image
#创建label组件
self.image=PhotoImage(file="1.png")
self.lable=Label(self,image=self.image)
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)
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")
10. Change the cursor shape. cursor
Note: The picture comes from the Internet.