目录
前言
Frame与Labelframe都是容器,用来存放其他控件,也是用来更好的管理布局。
我一般是用来存放一组相关的控件,让Frame与Labelframe成为其他控件的父控件。
(一)Frame
(1)说明:
tk.Frame(master, option*)
一般用颜色来区分Frame的位置
(2)源代码:
import tkinter as tk
win = tk.Tk()
# 定义第一个容器
frame_left = tk.Frame(win, bg="green")
frame_left.place(relx=0.2, rely=0.2, relwidth=0.3, relheight=0.6)
label_1 = tk.Label(frame_left, text="富强")
label_1.place(relx=0.2, rely=0.2)
label_2 = tk.Label(frame_left, text="民主")
label_2.place(relx=0.6, rely=0.2)
label_3 = tk.Label(frame_left, text="文明")
label_3.place(relx=0.2, rely=0.6)
label_4 = tk.Label(frame_left, text="和谐")
label_4.place(relx=0.6, rely=0.6)
# 定义第二个容器
frame_right = tk.Frame(win, bg="yellow")
frame_right.place(relx=0.5, rely=0.2, relwidth=0.3, relheight=0.6)
label_1 = tk.Label(frame_right, text="自由")
label_1.place(relx=0.2, rely=0.2)
label_2 = tk.Label(frame_right, text="平等")
label_2.place(relx=0.6, rely=0.2)
label_3 = tk.Label(frame_right, text="公正")
label_3.place(relx=0.2, rely=0.6)
label_4 = tk.Label(frame_right, text="法治")
label_4.place(relx=0.6, rely=0.6)
win.mainloop()
(3)输出效果:
(二)Labelframe
(1)说明:
大体上,Labelframe与Frame属性选项一样。
Labelframe有两点不同于Frame:
- Labelframe默认的边框使tk.GROOVE,relief=tk.GROOVE
- Labelframe还可以再边框写文字,即:text=”文字”
边框文字的位置:
默认是:labelanchor=”wn”
(2)源代码:
import tkinter as tk
win = tk.Tk()
# 定义第一个容器
frame_left = tk.LabelFrame(win, text="国家层面", labelanchor="n")
frame_left.place(relx=0.2, rely=0.2, relwidth=0.3, relheight=0.6)
label_1 = tk.Label(frame_left, text="富强")
label_1.place(relx=0.2, rely=0.2)
label_2 = tk.Label(frame_left, text="民主")
label_2.place(relx=0.6, rely=0.2)
label_3 = tk.Label(frame_left, text="文明")
label_3.place(relx=0.2, rely=0.6)
label_4 = tk.Label(frame_left, text="和谐")
label_4.place(relx=0.6, rely=0.6)
# 定义第二个容器
frame_right = tk.LabelFrame(win, text="社会层面", labelanchor="n")
frame_right.place(relx=0.5, rely=0.2, relwidth=0.3, relheight=0.6)
label_1 = tk.Label(frame_right, text="自由")
label_1.place(relx=0.2, rely=0.2)
label_2 = tk.Label(frame_right, text="平等")
label_2.place(relx=0.6, rely=0.2)
label_3 = tk.Label(frame_right, text="公正")
label_3.place(relx=0.2, rely=0.6)
label_4 = tk.Label(frame_right, text="法治")
label_4.place(relx=0.6, rely=0.6)
win.mainloop()