A, TreeView presentation
TreeView component table is a tree structure and combination. The first column is a tree structure, columns after a list. Each row represents one item, the item may be hierarchical tree, each item has a child item, the name of the corresponding text label. Value for each row of values represented by the tuple values.
There are five item labels are: text, image, values, open, tags.
Second, Parameter Description
1, TreeView parameters:
parameter | effect |
---|---|
columns | Value is a list. List each element represents a name identifier column. Length of the list is the length of the column. |
displaycolumns | List is a list of elements where the character, which represents the column and the order may be displayed, or use '#all' represents All |
height | Indicates the data lines (note: this width part is based on how many columns are automatically defined) |
padding | Filling, is a list of a maximum of four elements |
selectmode | Define how to select a row. "Extended" is an optional multi-line (with Ctrl + mouse), "browse" option is only one line, "none" can not be changed, the default setting is "extended" |
show | Which indicates the function of this display member, "tree" represents only the first column of the display (single-tree pattern), "headings" denotes a display, among other columns (single-mode list) a default is "tree headings", all the columns. Note that "# 0" (first column) always there is |
2, item parameters:
Options | description |
---|---|
text | The name of the tree here. |
image | Tree on the left side of the map to add a name. |
values | List structure here values for each row, column values unassigned null value exceeds the column length will be truncated. |
open | Boolean value that represents the sub-item display open or closed |
tags | Tag associated with the item |
3, tag parameters
Options | description |
---|---|
foreground | Foreground |
background | Background color |
font | Fonts |
image | Map |
Three, TreeView optional events, and methods
<< TreeviewSelect >>, representing the selected change occurred;
<< >> TreeviewOpen, Item's open = True occur when
<< TreeviewClose >>, item of open = False occurs when
TIP: Treeview.focus () and Treeview.selection () available item or items.
List of methods:
bbox (item, column = None)
Returns a range of item (x, y, width, height ), a column if the column is specified, a range of elements is returned, if the item is not visible, null is returned.
get_children (item = None)
returns a sub-item of item all, this is a list of sub-item form, if the item is not specified, the root of the returned item
set_children (item, * newchildren)
set up a new sub-item of the item. After you set up is actually replace all here
column(column, option=None, **kw)
给各列设置属性,或返回属性。
第一个column是列标识符
第二个option,如果不设置则返回所有属性的字典,如果设置则返回那个属性的值。
kw里的option有5个
id:只读属性,返回列名。
anchor:文字在cell里的对齐方式,标准的tk的anchor属性
minwidth: 值,单位是像素,列的最小宽度
stretch: 布尔值,表示列的宽度是否随整个部件的改动而变化。
width:列宽,单位是像素。
提示:如果要设置树状结构那列,用column=“#0”
delete(*items)
删除item及其子item
detach(*items)
断开item及其子item,这里的断开只是不显示,以后还可以链接起来。
exists(item)
返回True,如果item在树里。
focus(item=None)
如果不指定item,则返回当前获得焦点的item,如果指定item,则让该item获得焦点。若无则返回空值。
heading(column, option=None, **kw)
查询或修改指定列的标题选项
第一个column是列标识符
第二个option,如果不设置则返回所有属性的字典,如果设置则返回那个属性的值。
kw里的option有4个
text:列头名
image: 列头名右的图像
anchor:文字在heading里的对齐方式,标准的tk的anchor属性
command:点击列头的回调函数
insert(parent, index, iid=None, **kw)
创建新item并返回新创建item的项标识符。
parent:用item ID表示父item,或者‘’表示根item
index:数值int,或‘end’,表示item插入的位置
iid:item标识符,可自动生成
kw:看上面的Item Options介绍。
item(item, option=None, **kw)
查询或修改指定item的选项
selection(selop=None, items=None)
如果没指定selop则返回所有选中的items,列表形式,若selop指定了selection methods,则相应act。
set(item, column=None, value=None)
指定item,如果不设定column和value,则返回他们的字典,如果设定了column,则返回该column的value,如果value也设定了,则作相应更改。
四、代码示例
1、表格代码:
import tkinter as tk from tkinter import ttk window = tk.Tk() # 设置窗口大小 winWidth = 600 winHeight = 400 # 获取屏幕分辨率 screenWidth = window.winfo_screenwidth() screenHeight = window.winfo_screenheight() x = int((screenWidth - winWidth) / 2) y = int((screenHeight - winHeight) / 2) # 设置主窗口标题 window.title("TreeView参数说明") # 设置窗口初始位置在屏幕居中 window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y)) # 设置窗口图标 window.iconbitmap("./image/icon.ico") # 设置窗口宽高固定 window.resizable(0, 0) # 定义列的名称 columns = ("name", "gender", "age") tree = ttk.Treeview(window, show = "headings", columns = columns, selectmode = tk.BROWSE) # 设置表格文字居中 tree.column("name", anchor = "center") tree.column("gender", anchor = "center") tree.column("age", anchor = "center") # 设置表格头部标题 tree.heading("name", text = "姓名") tree.heading("gender", text = "性别") tree.heading("age", text = "年龄") # 设置表格内容 lists = [{"name": "yang", "gender": "男", "age": "18"}, {"name": "郑", "gender": "女", "age": "25"}] i = 0 for v in lists: tree.insert('', i, values = (v.get("name"), v.get("gender"), v.get("age"))) i += 1 tree.pack(expand = True, fill = tk.BOTH) # 获取当前点击行的值 def treeviewClick(event): # 单击 for item in tree.selection(): item_text = tree.item(item, "values") print(item_text) # 鼠标左键抬起 tree.bind('<ButtonRelease-1>', treeviewClick) # 鼠标选中一行回调 def selectTree(event): for item in tree.selection(): item_text = tree.item(item, "values") print(item_text) # 选中行 #tree.bind('<<TreeviewSelect>>', selectTree) window.mainloop()
2、树状代码:
import tkinter as tk from tkinter import ttk window = tk.Tk() # 设置窗口大小 winWidth = 600 winHeight = 400 # 获取屏幕分辨率 screenWidth = window.winfo_screenwidth() screenHeight = window.winfo_screenheight() x = int((screenWidth - winWidth) / 2) y = int((screenHeight - winHeight) / 2) # 设置主窗口标题 window.title("TreeView参数说明") # 设置窗口初始位置在屏幕居中 window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y)) # 设置窗口图标 window.iconbitmap("./image/icon.ico") # 设置窗口宽高固定 window.resizable(0, 0) # 定义列的名称 tree = ttk.Treeview(window, show = "tree") myid=tree.insert("",0,"中国",text="中国China",values=("1")) # ""表示父节点是根 myidx1=tree.insert(myid,0,"广东",text="中国广东",values=("2")) # text表示显示出的文本,values是隐藏的值 myidx2=tree.insert(myid,1,"江苏",text="中国江苏",values=("3")) myidy=tree.insert("",1,"美国",text="美国USA",values=("4")) myidy1=tree.insert(myidy,0,"加州",text="美国加州",values=("5")) # 鼠标选中一行回调 def selectTree(event): for item in tree.selection(): item_text = tree.item(item, "values") print(item_text) # 选中行 tree.bind('<<TreeviewSelect>>', selectTree) tree.pack(expand = True, fill = tk.BOTH) window.mainloop()
五、效果图