举例一个文本左对齐的案例
txt_1 = tk.Label(text='我是左对齐1111111')
txt_1.grid(row=0, column=0, sticky='W')
sticky参数有以下几种情况:
-
sticky=‘’:默认情况,控件在单元格中居中对齐。
-
sticky=‘N’:控件在单元格中上对齐。
-
sticky=‘S’:控件在单元格中下对齐。
-
sticky=‘E’:控件在单元格中右对齐。
-
sticky=‘W’:控件在单元格中左对齐。
-
sticky=‘NW’:控件在单元格中左上对齐。
-
sticky=‘NE’:控件在单元格中右上对齐。
-
sticky=‘SW’:控件在单元格中左下对齐。
-
sticky=‘SE’:控件在单元格中右下对齐。
这里演示较为常用的三种对齐方式,其他对齐方式根据需求调用
import tkinter as tk
root = tk.Tk()
root.title('控件的对齐方式')
root.geometry("300x400")
txt_1 = tk.Label(text='我是左对齐1111111')
txt_1.grid(row=0, column=0, sticky='W')
txt_2 = tk.Label(text='我是右对齐')
txt_2.grid(row=1, column=0, sticky='E')
txt_3 = tk.Label(text='我是居中对齐')
txt_3.grid(row=2, column=0, sticky='')
root.mainloop()
效果如下