import tkinter as tk
# Create the main window
root = tk.Tk()
root.geometry("400x250") # Set window size
root.title("Welcome to My App") # Set window title
# Create a StringVar to associate with the label
text_var = tk.StringVar()
text_var.set("Hello, World!")
# Create the label widget with all options
label = tk.Label(root,
textvariable=text_var,
anchor=tk.CENTER,
bg="lightblue",
height=3,
width=30,
bd=3,
font=("Arial", 16, "bold"),
cursor="hand2",
fg="red",
padx=15,
pady=15,
justify=tk.CENTER,
relief=tk.RAISED,
underline=0,
wraplength=250
)
# Pack the label into the window
label.pack(pady=20) # Add some padding to the top
# Run the main event loop
root.mainloop()
Syntax: Label ( master, option )
anchor: This option is used to control the positioning of the text if the widget has more space than required for the text. The default is anchor=CENTER, which centers the text in the available space.
bg: This option is used to set the normal background color displayed behind the label and indicator.
height:This option is used to set the vertical dimension of the new frame.
width:Width of the label in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
bd:This option is used to set the size of the border around the indicator. Default bd value is set on 2 pixels.
font:If you are displaying text in the label (with the text or textvariable option), the font option is used to specify in what font that text in the label will be displayed.
cursor:It is used to specify what cursor to show when the mouse is moved over the label. The default is to use the standard cursor.
textvariable: As the name suggests it is associated with a Tkinter variable (usually a StringVar) with the label. If the variable is changed, the label text is updated.
bitmap:It is used to set the bitmap to the graphical object specified so that, the label can represent the graphics instead of text.
fg:The label clior, used for text and bitmap labels. The default is system specific. If you are displaying a bitmap, this is the clior that will appear at the position of the 1-bits in the bitmap.
image: This option is used to display a static image in the label widget.
padx:This option is used to add extra spaces between left and right of the text within the label.The default value for this option is 1.
pady:This option is used to add extra spaces between top and bottom of the text within the label.The default value for this option is 1.
justify:This option is used to define how to align multiple lines of text. Use LEFT, RIGHT, or CENTER as its values. Note that to position the text inside the widget, use the anchor option. Default value for justify is CENTER.
relief: This option is used to specify appearance of a decorative border around the label. The default value for this option is FLAT.
underline:This
wraplength:Instead of having only one line as the label text it can be broken into to the number of lines where each line has the number of characters specified to this option.
anchor: Tùy chọn này được sử dụng để kiểm soát vị trí của văn bản nếu tiện ích có nhiều không gian hơn mức cần thiết cho văn bản. Mặc định là anchor=CENTER (mỏ neo=CENTER), giúp căn giữa văn bản trong khoảng trống có sẵn.
bg: Tùy chọn này được sử dụng để thiết lập màu nền bình thường hiển thị phía sau nhãn và chỉ báo.
chiều cao: Tùy chọn này được sử dụng để thiết lập kích thước dọc của khung mới.
width: Chiều rộng của nhãn tính bằng ký tự (không phải pixel!). Nếu tùy chọn này không được đặt, nhãn sẽ được định cỡ sao cho vừa với nội dung.
bd: Tùy chọn này được sử dụng để thiết lập kích thước đường viền xung quanh chỉ báo. Giá trị bd mặc định được đặt là 2 pixel.
phông chữ: Nếu bạn đang hiển thị văn bản trong nhãn (với tùy chọn văn bản hoặc biến văn bản), tùy chọn phông chữ được sử dụng để chỉ định phông chữ mà văn bản trong nhãn sẽ được hiển thị.
Con trỏ: Dùng để chỉ định con trỏ nào sẽ hiển thị khi di chuyển chuột qua nhãn. Mặc định là sử dụng con trỏ chuẩn.
textvariable: Đúng như tên gọi, biến này được liên kết với một biến Tkinter (thường là StringVar) cùng với nhãn. Nếu biến thay đổi, văn bản nhãn sẽ được cập nhật.
bitmap: Được sử dụng để thiết lập bitmap cho đối tượng đồ họa được chỉ định sao cho nhãn có thể biểu diễn đồ họa thay vì văn bản.
fg: Nhãn clior, dùng cho nhãn văn bản và nhãn bitmap. Mặc định là tùy thuộc vào hệ thống. Nếu bạn đang hiển thị bitmap, đây là clior sẽ xuất hiện tại vị trí của các bit-1 trong bitmap.
hình ảnh: Tùy chọn này được sử dụng để hiển thị hình ảnh tĩnh trong tiện ích nhãn.
padx: Tùy chọn này được sử dụng để thêm khoảng cách giữa bên trái và bên phải của văn bản trong nhãn. Giá trị mặc định cho tùy chọn này là 1.
pady: Tùy chọn này được sử dụng để thêm khoảng cách giữa đầu và cuối văn bản trong nhãn. Giá trị mặc định cho tùy chọn này là 1.
justify: Tùy chọn này được sử dụng để xác định cách căn chỉnh nhiều dòng văn bản. Sử dụng TRÁI, PHẢI hoặc GIỮA làm giá trị của nó. Lưu ý rằng để định vị văn bản bên trong tiện ích, hãy sử dụng tùy chọn neo. Giá trị mặc định cho justify là GIỮA.
Độ nổi: Tùy chọn này được sử dụng để chỉ định hình dạng của đường viền trang trí xung quanh nhãn. Giá trị mặc định cho tùy chọn này là PHẲNG.
gạch chân: Đây
wraplength: Thay vì chỉ có một dòng làm văn bản nhãn, nhãn có thể được chia thành nhiều dòng, trong đó mỗi dòng có số ký tự được chỉ định cho tùy chọn này.