Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách căn chỉnh văn bản trong Label Tkinter Python

Widget Label Tkinter thêm ảnh và tạo text. Có hàm/phương thức định kiểu widget. Để căn chỉnh text trong Label, dùng justify. Dùng căn vị trí/căn chỉnh: RIGHT, LEFT, CENTER.

Ví dụ

Ứng dụng căn vị trí text label bằng justify.

#Import tkinter library
from tkinter import *
#Tạo instance tkinter frame
win= Tk()
#Đặt geometry
win.geometry("750x350")
#Tạo Label widget
label1= Label(win, text="Box1")
label1.pack()
label2= Label(win, text= "\nKeep\nLearning", bd=1, relief= "solid",font= ("Helvetica 20"), justify= RIGHT)
label2.pack()
Label(win, text= "Box2").pack()
label3= Label(win, text="\nLearning\nMakes\nPerfect", bd=1, relief="solid", font=('Helvetica 20'), justify= LEFT)
label3.pack()
win.mainloop()

Kết quả

Chạy mã hiển thị cửa sổ label widget có text. Căn chỉnh trực quan bằng justify.

Cách căn chỉnh văn bản trong Label Tkinter Python