网站首页 > 技术文章 正文
在本文中,将介绍如何使用 tkinter PanedWindow 窗格窗口小部件。PanedWindow 小部件是一个容器,可以在窗体上创建可以调节大小的区域,这些区域称作窗格。
要创建小组件,请使用以下语法:
tk.PanedWindow(master, **options)
PanedWindow 常用选项:
选项 | 描述 |
bd | 3D边框大小,默认不包含边框 |
bg | 背景颜色 |
cursor | 指定鼠标光标类型 |
borderwidth | 边框宽度,默认值为 2 像素 |
handlepad | 手柄和窗口边框距离,默认值为 8 像素 |
height | 小部件高度 |
handlesize | 手柄的大小,默认值为 8 像素 |
orient | 并排放置子窗口,设置为 HORIZONTAL,从上到下放置子窗口,设置为 VERTICAL |
sashpad | 窗格填充 |
sashwidth | 窗格边框宽度 |
sashrelief | 窗格边框类型 |
showhandle | 是否显示手柄,True/False |
width | 小组件的宽度 |
relief | 边框的类型,默认值为 FLAT |
PanedWindow 常用方法:
方法 | 描述 |
add(child,options) | 为窗格添加其他小部件 |
forget(child) | 删除子部件 |
panecget(child, option) | 获得子组件指定选项的值 |
paneconfig(child, **options) | 设置子组件的各种选项 |
PanedWindow 方法选项:
选项 | 描述 |
after | 添加新的子组件到指定子组件后边 |
before | 添加新的子组件到指定子组件前边 |
height | 指定子组件的高度 |
minsize | 控制窗格宽度/高度不得低于的值 |
padx | 指定子组件的水平间距 |
pady | 指定子组件的垂直间距 |
sticky | 指定子组件位于窗格的位置(e、s、w、n,以及组合值) |
width | 指定子组件的宽度 |
示例:创建可调横向和纵向窗格
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="标签 1", bg="blue")
pw1.add(left_Label, width=200)
pw2 = tk.PanedWindow(pw, orient=tk.VERTICAL, showhandle=True)
top_Label = tk.Label(pw2, text="标签 2", bg="green")
pw2.add(top_Label, height=200)
bottom_Label = tk.Label(pw2, text="标签 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="标签", 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="标签 1", bg="blue")
pw1.add(left_Label, width=200)
pw2 = tk.PanedWindow(pw, orient=tk.VERTICAL, showhandle=True)
top_Label = tk.Label(pw2, text="标签 2", bg="green")
pw2.add(top_Label, height=200)
bottom_Label = tk.Label(pw2, text="标签 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="设置", command=conf).pack(side=tk.LEFT, padx=10)
root.mainloop()
add() 方法:为左侧窗格再添加 Label 小部件
forget() 方法:删除左侧窗格的 left_Label 小部件
panecget() 方法: 获取 top_Label 组件 height 选项的值,输出到控制台
paneconfig() 方法:设置 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') # 创建纵向窗格
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') # 创建横向窗格
label = tk.Label(pw1,text='左侧标签',width=15)
right_frame = tk.Frame(pw1)
text = tk.Text(right_frame,wrap="none") # 右侧多行文本框
scrollbar_h = tk.Scrollbar(right_frame,orient=tk.HORIZONTAL,command=text.xview) # 创建滚动条
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()
猜你喜欢
- 2025-07-23 python tkinter tk窗口组件的基础用法介绍
- 2025-07-23 一起学python:Tkinter——轻装上阵,快速入门
- 2025-07-23 Python GUI 编程:tkinter 初学者入门指南——Ttk 小部件
- 2025-07-23 Python,Tkinter的基础使用(python3 tkinter)
- 2025-07-23 图文并茂:Python Tkinter从入门到高级实战全解析
- 2025-07-23 python tkinter桌面应用gui库基础介绍
- 2025-07-23 Python tkinter写个画图板,一个坑和实用知识解析
- 2025-07-23 python tkinter组件概述(python的tkinter模块详解)
- 283℃Python短文,Python中的嵌套条件语句(六)
- 279℃PythonNet:实现Python与.Net代码相互调用!
- 279℃python笔记:for循环嵌套。end=""的作用,图形打印
- 274℃Python实现字符串小写转大写并写入文件
- 273℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 133℃原来2025是完美的平方年,一起探索六种平方的算吧
- 118℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 111℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 最近发表
-
- python tkinter tk窗口组件的基础用法介绍
- 一起学python:Tkinter——轻装上阵,快速入门
- Python GUI 编程:tkinter 初学者入门指南——窗格窗口
- Python GUI 编程:tkinter 初学者入门指南——Ttk 小部件
- Python,Tkinter的基础使用(python3 tkinter)
- 图文并茂:Python Tkinter从入门到高级实战全解析
- python tkinter桌面应用gui库基础介绍
- Python tkinter写个画图板,一个坑和实用知识解析
- python tkinter组件概述(python的tkinter模块详解)
- Flask中使用Blinker实现信号传递(flask传数据给前端)
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)