简要描述:使Label组件里面的文本左对齐
# 构造:
anchor='w' # 文本区域左对齐,可填'n','s','e',w'
justify='left' # 行左对齐,可填'left','right','center'
# 布局:
.grid(sticky='w') # 在单元格内左对齐,仅限grid,可填'n','s','w','e'或自由搭配
1代码:
import tkinter
root = tkinter.Tk()
root.configure(bg='black')
label1 = tkinter.Label(root,text='label1\nmy name is label1',width=20)
label1.grid(row=0,column=0)
label2 = tkinter.Label(root,text='label2\nmy name is label2',width=20,anchor='w')
label2.grid(row=1,column=0)
label3 = tkinter.Label(root,text='label3\nmy name is label3',width=20,anchor='w',justify='left')
label3.grid(row=2,column=0)
label4 = tkinter.Label(root,text='label4\nmy name is label4',width=20,anchor='w',justify='left')
label4.grid(row=3,column=0,sticky='nswe')
root.grid_columnconfigure(0,weight=1)
root.mainloop()
修改:
import tkinter as tk
wd_main = tk.Tk()
# wd_main.geometry("600x300")
lb1 = tk.Label(wd_main, text = "Once upon a time a girl...",
relief = "groove", width = 60).pack(side = "left")
# lb2 = tk.Label(wd_main, text = "test2kasjfkd",
# relief = "groove").pack(side = "left", anchor = "nw")
wd_main.mainloop()
可改为
import tkinter as tk
wd_main = tk.Tk()
# wd_main.geometry("600x300")
lb1 = tk.Label(wd_main, text = "Once upon a time a girl...",
relief = "groove", width = 60,anchor = 'w').pack()
wd_main.mainloop()
2、anchor和justify有什么区别:
justify是 行之间 的对齐
anchor是所有行在 整个label 中的 位置
里面框起来的对齐是justify
外面的是anchor
3、root.grid_ columnconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
如果你是grid布局的话,这个用于将root内的第0列横向填充,权重为1,root可以是任何控件
还有root.grid_rowconfigure(0, weight=1)则是第0行纵向填充
原来:
设置填充:
root.grid_rowconfigure(1,weight=1)
root.grid_columnconfigure(0,weight=1)
现在: