Tkinter (14) 可调尺寸窗格部件 PanedWindow
可调尺寸窗格部件的创建及其选项
可调尺寸窗格部件作为一个容器的部件, 其内部的子部件之间有窗格, 窗格具有窗柄(handle) 及窗格线(sash), 可以提供使用者调整各部件所占区域的大小.
import tkinter as tk
parent = tk.Tk()
pane = tk.PanedWindow(parent, option, ...)
选项 | 说明 |
---|---|
bg or background | 背景色 |
bd or borderwidth | 框的寛度, 内定为 2 点素 |
cursor | 当鼠标移到部件时, 所显示的鼠标图示 |
handlepad | 窗格线起点与窗柄的距离点素, 内定为 8 点素 |
handlesize | 正方形窗柄的边长点素, 内定为 8 点素 |
height | 部件高度, 未指定则依子部件高度而定 |
opaqueresize | 即时更新窗格尺寸的变更 (True), 或拖弋后才更新 (False) |
orient | 内部部件的摆放方向, VERTICAL 或 HORIZONTAL |
relief | 花边样式,内定为 FLAT |
sashpad | 窗格线两端的间距, 内定为 0 点素 |
sashrelief | 窗格线的样式, 内定为 FLAT |
sashwidth | 窗格线的寛度, 内定为 2 点素 |
showhandle | 是否显示窗柄, 内定为否 |
width | 部件寛度, 未指定则依子部件寛度而定 |
增加部件到 PaneWindow 中
新增部件的方式为以 PaneWindow 为父部件建立子部件后, 再以父部件的 add 方法加入, 而不是使用几何布局 (grid, place, pack) 的方式.
可调尺寸窗格部件的方法
方法 & 说明 |
---|
add(child[, option=value] …) 新增子部件及其外观位置的选项 |
forget(child) 移除子部件 |
identify(x, y) 检视在座标(x, y) 处的对象, (1) 子部件返回空字符串(2) 窗柄返回(n, ‘handle’) (3) 窗格线(n, ‘sash’), n 代表其顺序, 从0 开始 |
panecget(child, option) 取得子部件的选项值 |
paneconfig(child, option=value, …) 设置子部件的外观位置选项值 |
panes() 返回子部件列表 |
remove(child) 移除子部件, 同 forget 方法 |
sash_coord(index) 返回窗格线左上角的座标点 (x, y) |
sash_place(index, x, y) |
可调尺寸窗格部件方法的选项
选项 | 说明 |
---|---|
after | 增加在某一个部件之后, 内定为最后面的部件之后 |
before | 增加在某一个部件之前, 内定为最后面的部件之后 |
height | 子部件的高度 |
minsize | 设置最小的寛度 (HORIZONTAL) 或高度 (VERTICAL) |
padx | 子部件的左右间隔 |
pady | 子部件的上下间隔 |
sticky | 子部件在窗格中的位置, 如同 grid 的 sticky 选项 |
width | 子部件的寛度 |
范例视窗及代码
import tkinter as tk
root = tk.Tk()
root.wm_title("PaneWindow Demo")
font = ('Courier New', 20, 'bold')
pane_1 = tk.PanedWindow(root, showhandle=True, orient=tk.HORIZONTAL, sashrelief=tk.RAISED)
label_11 = tk.Label(pane_1, text="Pane_1\nLabel 1", font=font, width=20)
pane_2 = tk.PanedWindow(pane_1, showhandle=True, orient=tk.VERTICAL, sashrelief=tk.RAISED)
pane_1.add(label_11)
pane_1.add(pane_2)
label_21 = tk.Label(pane_2, text="Pane_2\nLabel 1", font=font, width=20)
label_22 = tk.Label(pane_2, text="Pane_2\nLabel 2", font=font, width=20)
pane_2.add(label_21)
pane_2.add(label_22)
pane_1.grid(row=0, column=0)
root.mainloop()
本作品采用《CC 协议》,转载必须注明作者和本文链接