Tkinter 标签小部件可以使用锚点属性对齐。为了计算小部件的容纳间距和对齐方式,anchor 会以更好的方式提供帮助。Anchor提供了N、W、S、E、NW、NE等多种选择。可以在包管理器本身中定义的 SW、SE。
在下面的示例中,我们将通过向“w”方向添加锚点属性来将应用程序的标签文本左对齐。
#Import the required library from tkinter import* #Create an instance of tkinter frame win= Tk() #Set the geometry win.geometry("750x250") #Create a Label Widget Label(win, text= "New Line Text", font= ('Helvetica 15 underline'), background="gray74").pack(pady=20, side= TOP, anchor="w") win.mainloop()输出结果
运行上面的代码将使文本标签小部件向左对齐。