Tkinter Widget Anchor
Tkinter Anchor actually refers to the setting of the output position of the label text in the label area. By default the Widget control is centered up and down and left and right, see the implementation result of ch2_4.py. We can also use the anchor option to set the alignment of the widget control as shown in the following figure.
Example#1
Use anchor to make the string output from the top left position of the label interval.
from tkinter import *
root = Tk()
root.title("apidemos.com")
label = Label(root,text="I like tkinter",
fg="red",bg="yellow",
height=30,width=150,
anchor="nw")
label.pack() # Packaging and positioning components
# print(type(label)) # <class 'NoneType'>
root.mainloop()
Output:
Example#2
Let the string be output at the bottom right of the label.
from tkinter import *
root = Tk()
root.title("apidemos.com")
label = Label(root,text="I like tkinter",
fg="red",bg="yellow",
height=30,width=150,
anchor=SE)
label.pack() # Packaging and positioning components
# print(type(label)) # <class 'NoneType'>
root.mainloop()
Output:
The anchor parameter settings can also use built-in uppercase constants, for example, nw uses NW, n uses N, ne uses NE, w uses W, center uses CENTER, e uses E, sw uses SW, s uses S, and se uses SE. When the program uses uppercase constants, the double quotes of the string can be omitted.