在本文中,將介紹如何使用 tkinter PanedWindow 窗格窗口小部件。PanedWindow 小部件是一個容器,可以在窗體上創(chuàng)建可以調(diào)節(jié)大小的區(qū)域,這些區(qū)域稱作窗格。
要創(chuàng)建小組件,請使用以下語法:
tk.PanedWindow(master, **options)
PanedWindow 常用選項:
| |
---|
bd | |
bg | |
cursor | |
borderwidth | |
handlepad | |
height | |
handlesize | |
orient | 并排放置子窗口,設(shè)置為 HORIZONTAL,從上到下放置子窗口,設(shè)置為 VERTICAL |
sashpad | |
sashwidth | |
sashrelief | |
showhandle | |
width | |
relief | |
PanedWindow 常用方法:
| |
---|
add(child,options) | |
forget(child) | |
panecget(child, option) | |
paneconfig(child, **options) | |
PanedWindow 方法選項:
| |
---|
| |
| |
| |
| |
| |
| |
| 指定子組件位于窗格的位置(e、s、w、n,以及組合值) |
| |
示例:創(chuàng)建可調(diào)橫向和縱向窗格
import tkinter as tk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('PanedWindow 窗格窗口演示')
pw = tk.PanedWindow(root, orient=tk.HORIZONTAL, showhandle=True)
pw1 = tk.PanedWindow(root, orient=tk.HORIZONTAL, showhandle=True)
left_Label = tk.Label(pw1, text="標(biāo)簽 1", bg="blue")
pw1.add(left_Label, width=200)
pw2 = tk.PanedWindow(pw, orient=tk.VERTICAL, showhandle=True)
top_Label = tk.Label(pw2, text="標(biāo)簽 2", bg="green")
pw2.add(top_Label, height=200)
bottom_Label = tk.Label(pw2, text="標(biāo)簽 3", bg="red")
pw2.add(bottom_Label)
pw.add(pw1)
pw.add(pw2)
pw.pack(fill=tk.BOTH, expand=True)
root.mainloop()
示例 :窗格窗口常用方法演示
import tkinter as tk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('PanedWindow 窗格窗口演示')
def add():
label = tk.Label(pw1, text="標(biāo)簽", bg="lightblue")
pw1.add(label, before=left_Label, width=80, minsize=50)
def remove():
pw1.forget(left_Label)
def get():
print(pw2.panecget(top_Label, 'height'))
def conf():
pw2.paneconfig(top_Label, height=80)
pw = tk.PanedWindow(root, orient=tk.HORIZONTAL, showhandle=True)
pw1 = tk.PanedWindow(root, orient=tk.HORIZONTAL, showhandle=True)
left_Label = tk.Label(pw1, text="標(biāo)簽 1", bg="blue")
pw1.add(left_Label, width=200)
pw2 = tk.PanedWindow(pw, orient=tk.VERTICAL, showhandle=True)
top_Label = tk.Label(pw2, text="標(biāo)簽 2", bg="green")
pw2.add(top_Label, height=200)
bottom_Label = tk.Label(pw2, text="標(biāo)簽 3", bg="red")
pw2.add(bottom_Label)
pw.add(pw1)
pw.add(pw2)
pw.pack(fill=tk.BOTH, expand=True)
frame = tk.Frame(root)
frame.pack()
tk.Button(frame, text="增加", command=add).pack(side=tk.LEFT, padx=10)
tk.Button(frame, text="刪除", command=remove).pack(side=tk.LEFT, padx=10)
tk.Button(frame, text="獲取", command=get).pack(side=tk.LEFT, padx=10)
tk.Button(frame, text="設(shè)置", command=conf).pack(side=tk.LEFT, padx=10)
root.mainloop()
add()
方法:為左側(cè)窗格再添加Label
小部件
forget()
方法:刪除左側(cè)窗格的left_Label
小部件
panecget()
方法: 獲取top_Label
組件height
選項的值,輸出到控制臺
paneconfig()
方法:設(shè)置 top_Label
組件height
選項的值為 80
示例:窗格窗口中Text增加滾動條
import tkinter as tk
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('PanedWindow 窗格窗口演示')
pw=tk.PanedWindow(root,orient=tk.VERTICAL,sashrelief='sunken') # 創(chuàng)建縱向窗格
pw.pack(fill=tk.BOTH,expand=True)
top_frame = tk.Frame(pw)
tk.Label(top_frame, text='頂部Frame').pack()
pw1=tk.PanedWindow(pw,orient=tk.HORIZONTAL,sashrelief='sunken') # 創(chuàng)建橫向窗格
label = tk.Label(pw1,text='左側(cè)標(biāo)簽',width=15)
right_frame = tk.Frame(pw1)
text = tk.Text(right_frame,wrap="none") # 右側(cè)多行文本框
scrollbar_h = tk.Scrollbar(right_frame,orient=tk.HORIZONTAL,command=text.xview) # 創(chuàng)建滾動條
scrollbar_v = tk.Scrollbar(right_frame,command=text.yview)
scrollbar_h.pack(side=tk.BOTTOM,fill=tk.X)
scrollbar_v.pack(side=tk.RIGHT,fill=tk.Y)
text.pack(side=tk.LEFT,fill=tk.BOTH,expand=True)
text.config(xscrollcommand=scrollbar_h.set,yscrollcommand=scrollbar_v.set)
pw1.add(label)
pw1.add(right_frame)
pw.add(top_frame,minsize=80)
pw.add(pw1)
root.mainloop()