open3d高级UI控件:树形图

文章目录

前情提要:
Open3d GUI初步
Open3d 基础布局
Open3d 基础控件

TreeView

TreeView,顾名思义,就是树形图,一般来说,树的每个节点都只包含一个标签,其基本形式如下

在这里插入图片描述

代码为

import open3d as o3d
import open3d.visualization.gui as gui
import open3d.visualization.rendering as rendering
app = gui.Application.instance

app.initialize()
win = app.create_window(“tree view”, 500, 200)

tree = gui.TreeView()
tree.add_text_item(tree.get_root_item(), “uncle”)
father = tree.add_text_item(tree.get_root_item(), “father”)
big = tree.add_text_item(father, “big son”)
tree.add_text_item(big, “grandson 1”)
tree.add_text_item(big, “grandson 2”)
tree.add_text_item(big, “grandson 3”)
little = tree.add_text_item(father, “little son”)
tree.can_select_items_with_children = True
def _on_tree(self, item):
print(item)

tree.set_on_selection_changed(self._on_tree)
tree.selected_item = little
win.add_child(tree)
app.run()

其中,gui.TreeView用于生成一个树形图,add_text_item用以添加树形图的节点,其输入参数有二,分别是父节点和节点名称。

tree.add_text_item(tree.get_root_item(), "uncle")

 
 

上面这条代码中,tree.get_root_item()用于获取根节点,整体而言的意思是,在根节点下面添加一个子节点,标签为uncle

add_text_item函数返回的是新建子节点这个对象,由于uncle节点没有子节点,无需新建一个变量为其标识。而father有三个子节点,为了让他的两个儿子挂载到他这个节点上,需要新建一个变量名。

最后,为树形图添加了一个动作,即当选择的节点发生变化时,执行_on_tree这个函数。

树的成员can_select_items_with_children设为True,说明每个节点都可以被选中,如果设为False,那么当点击节点的标签时,会直接打开或者关闭这一层节点。当然这是官网说的,我其实并没有感觉出来二者有什么不同。

CheckableTextTreeCell

treeview的节点中,除了朴实无华的标签之外,还可以添加勾选框,只需将add_text_item替换为add_item,就可以添加CheckableTextTreeCell作为节点

效果为

在这里插入图片描述

代码如下

app = gui.Application.instance
app.initialize()
win = app.create_window("tree view", 500, 200)

check = lambda str : lambda isChecked : print(str, isChecked)

tree = gui.TreeView()
root = tree.get_root_item()
tree.add_item(root, gui.CheckableTextTreeCell(
“uncle”, False, check(“uncle”)))
father = tree.add_item(root, gui.CheckableTextTreeCell(
“father”, False, check(“father”)))
tree.add_item(father, gui.CheckableTextTreeCell(
“big son”, False, check(“big son”)))
tree.add_item(father,gui.CheckableTextTreeCell(
“little son”, False, check(“little son”)))
tree.can_select_items_with_children = True
def _on_tree(self, item):
print(item)

tree.set_on_selection_changed(self._on_tree)
tree.selected_item = little
win.add_child(tree)
app.run()

add_item的输入参数分别是父节点和子节点对应的对象,gui.CheckableTextTreeCell的输入参数有三个,分别是标签字符串,选框是否选中,以及一个回调函数。

考虑到学习open3d的同学一般都是老手,故而这里用了lambda表达式,相当于对每个节点都生成一个专门的回调函数,例如

check("uncle")相当于

# 由于是匿名函数,所以函数名姑且用_表示
def _(isChecked):
    print("uncle", isChecked)

 
 

其他Cell

ColormapTreeCell

只要学会了CheckableTextTreeCell,那么理解ColormapTreeCell也就毫无难度。

ColormapTreeCell的构造函数中包括四个输入参数,分别是

  • value:数值
  • color:颜色
  • on_value_changed:数字变化时的回调函数
  • on_color_changed:颜色变化时的回调函数

LUTTreeCell

LUTTreeCell就更加综合了,将选框和颜色都排到了树形图的节点中,故而其构造函数更加复杂,包括

  • text 文本内容
  • is_checked 选框是否选中
  • color 颜色
  • on_enabled 选框状态变化时的回调函数
  • on_color 颜色变化时的回调函数

考虑到add_item添加节点的数据类型为gui.Widget,故而绝对不限于这些专门用于树形图的数据类型,这里只是做一个简要的介绍,其实际应用可以更加炫酷。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值