Tkinter grid sticky parameter
sticky parameter is similar to anchor, but it can only be set to N/S/W/E, i.e. top/bottom/left/right alignment.
In principle, if widgets with the same column have different widths, the grid method will keep the widest control as the base, and the shorter control will be centered, see the following example.
Tkinter grid sticky Example#1
Observe that when widget controls in the same column have different widths, the contents of the controls are center-aligned.
from tkinter import *
window = Tk()
window.title("apidemos.com")
lab1 = Label(window,text="Label 1")
lab2 = Label(window,bg="yellow",width=20)
lab3 = Label(window,text="Label 22222")
lab4 = Label(window,bg="aqua",width=20)
lab1.grid(row=0,column=0,padx=5,pady=5)
lab2.grid(row=0,column=1,padx=5,pady=5)
lab3.grid(row=1,column=0,padx=5)
lab4.grid(row=1,column=1,padx=5)
window.mainloop()
Output:
You can see from the figure above that Label 1
is center-aligned.
Tkinter grid sticky Example#2
Set Label 1
to be aligned to the left.
from tkinter import *
window = Tk()
window.title("apidemos.com")
lab1 = Label(window,text="Label 1")
lab2 = Label(window,bg="yellow",width=20)
lab3 = Label(window,text="Label 22222")
lab4 = Label(window,bg="aqua",width=20)
lab1.grid(row=0,column=0,padx=5,pady=5,sticky=W)
lab2.grid(row=0,column=1,padx=5,pady=5)
lab3.grid(row=1,column=0,padx=5)
lab4.grid(row=1,column=1,padx=5)
window.mainloop()
Output:
The possible values of sticky parameter N/S/W/E can also be used in combination.
-
sticky=N+S: Stretches the height so that the control is aligned at the top and bottom.
-
sticky=W+E: you can stretch the width so that the control is aligned on the left and the right.
-
sticky=N+S+E: Stretch the height so that the control is aligned at the top and bottom, while tangentially aligning the right side.
-
sticky=N+S+W: Stretch the height to align the control at the top and bottom, and tidy the left side.
-
sticky=N+S+W+E: Stretch the height so that the control is aligned at the top and bottom, while tangentially aligning the left and right edges.
Tkinter grid sticky Example#3
Use the relief="raised"
parameter to increase the appearance of the Label.
from tkinter import *
window = Tk()
window.title("apidemos.com")
lab1 = Label(window,text="Label 1",relief="raised")
lab2 = Label(window,bg="yellow",width=20)
lab3 = Label(window,text="Label 2222222",relief="raised")
lab4 = Label(window,bg="aqua",width=20)
lab1.grid(row=0,column=0,padx=5,pady=5)
lab2.grid(row=0,column=1,padx=5,pady=5)
lab3.grid(row=1,column=0,padx=5)
lab4.grid(row=1,column=1,padx=5)
window.mainloop()
Output:
The purpose of the above procedure is mainly to understand the width of Label.
Tkinter grid sticky Example#4
Using the sticky=W+E
parameter, the following program is mainly to observe the result after Label 1
has been stretched.
from tkinter import *
window = Tk()
window.title("apidemos.com")
lab1 = Label(window,text="Label 1",relief="raised")
lab2 = Label(window,bg="yellow",width=20)
lab3 = Label(window,text="Label222222",relief="raised")
lab4 = Label(window,bg="aqua",width=20)
lab1.grid(row=0,column=0,padx=5,pady=5,sticky=W+E)
lab2.grid(row=0,column=1,padx=5,pady=5)
lab3.grid(row=1,column=0,padx=5)
lab4.grid(row=1,column=1,padx=5)
window.mainloop()
Output: