#Labelframe
Labelframe 是 Frame 容器的改进版,它允许为容器添加一个标签,该标签既可以是普通的文字标签,
也可以将任意 GUI 组件作为标签。
Labelframe 允许通过如下选项对标签进行定制:
labelwidget:设置可以将任意 GUI 组件作为标签。
labelanchor:设置标签的位置。该选项支持 'e'、's'、'w'、'n'、'es'、'ws'、'en'、'wn'、'ne'、'nw'、'se'、'sw' 这 12 个
边项值,用于控制标签的位置。
Panedwindow 是一个非常有特色的容器,它自带布局管理功能,它允许通过 orient 选项指定水平或垂直方向,
让容器中的各组件按水平或垂直方向排列。
在创建 Panedwindow 之后,程序可通过如下方法操作 Panedwindow 容器中的子组件:
add(self, child, **kw):添加一个子组件。
insert(self, pos, child,**kw):在pos 位置插入一个子组件。
remove(self, child):删除一个子组件,该子组件所在区域也被删除。
Panedwindow 组件是可以嵌套的,以实现功能更丰富的界面
from tkinter import *
from tkinter import ttk
root = Tk()
root.title('')
root.geometry('600x500')
#创建Frame
frame = Frame(root,bg='red')
frame.pack(fill=BOTH, expand=YES, padx=10, pady=10)
#创建LabelFrame
labelframe = LabelFrame(frame, text='LabelFrame',bg='green')
labelframe.pack(fill=BOTH, expand=YES, padx=10, pady=10)
bm = PhotoImage(file='C:\\Users\\caojiangtao\\Desktop\\Python.png')
label = Label(frame,text='LabelFrame', image=bm,compound=LEFT,width=200,height=30)
labelframe['labelwidget'] = label
def dirchange():
labelframe['labelanchor'] = directions[intVar.get()]
print(intVar.get())
intVar = IntVar()
directions = ['e', 's', 'w', 'n', 'es', 'ws', 'en', 'wn',
'ne', 'nw', 'se', 'sw']
i = 0
for direction in directions:
rb = Radiobutton(labelframe, text=direction,variable=intVar,value=i,command=dirchange)
rb.pack(side=LEFT,fill=X,expand=1,anchor=S)
i += 1
intVar.set(3)
# tkinter的pandwindow没能实现嵌套,ttk.PanedWindow可以
# #创建PandWindow
# pwindow = PanedWindow(root,orient=HORIZONTAL,background='blue',relief=RAISED)
# pwindow.pack(fill=BOTH, expand=1,padx=20,pady=20)
# Bn = Button(pwindow, text="按钮remove",command=lambda: pwindow.remove(Bn))
# pwindow.add(Bn)
#
# #嵌套
# rightwindow = PanedWindow(pwindow,orient=VERTICAL,background='white',relief=RAISED)
# rightwindow.pack(fill=BOTH, expand=1)
#
# label = Label(rightwindow, text="标签add",padx=10,pady=10,background='lightgreen',)
# rightwindow.add(label)
#
# entry = Entry(rightwindow,width=20)
# rightwindow.add(entry)
#
# # rightwindow.insert(1, Label(rightwindow, text='插入的标签'))
#创建PandWindow
style = ttk.Style()
style.configure("fkit.TPanedwindow",background='blue', relief=RAISED)
pwindow = ttk.Panedwindow(root,orient=HORIZONTAL, style="fkit.TPanedwindow")
pwindow.pack(fill=BOTH, expand=YES,padx=20,pady=20)
Bn = Button(pwindow, text="按钮remove",command=lambda: pwindow.remove(Bn))
pwindow.add(Bn)
# 创建嵌套Panedwindow
rightwindow = PanedWindow(pwindow, orient=VERTICAL)
pwindow.add(rightwindow)
label = Label(rightwindow, text="标签add",background='lightgreen')
rightwindow.add(label)
entry = Entry(rightwindow,width=20)
entry.insert(END,'Entry add')
rightwindow.add(entry)
#insert不能在嵌套的pandwindow里使用
pwindow.insert(1, Label(pwindow, text='插入标签'))
root.mainloop()