wxpython之tree_ctrl图标与点击事件

wxpython之tree_ctrl图标与点击事件


如何用wxpython展示一个如图所示的的树呢?
wxpython的treectrl功能可以实现。
1.首先我们可以用wxglade画前端frame界面,添加一个TreeCtrl ,或者多个TreeCtrl,添加root.每一个tree只能有一个root

self.tree_ctrl_1 = wx.TreeCtrl(self.panel_1, wx.ID_ANY)
my_defect_root =self.my_tree.AddRoot("My Defect")

2.然后给设置图片list 用来给根节点和树节点放不一样的图片

self.image_list = wx.ImageList(16, 16)
self.case_icon =self.image_list.Add(wx.Image("resource/image/icon/bookmark.png",
                                              wx.BITMAP_TYPE_PNG).Scale(16, 16).ConvertToBitmap())
self.folder_icon =self.image_list.Add(wx.Image("resource/image/icon/document.png",
                                                wx.BITMAP_TYPE_PNG).Scale(16, 16).ConvertToBitmap())

3.然后给树设置imagelist

self.project_tree.SetImageList(self.image_list)

#注意千万不能用AssignImageList ,这样写虽然运行没问题,但是关闭窗口时会导致crash,一定要用SetImageList
4.给每一个节点设置图片和文字

status_node =self.my_tree.AppendItem(my_defect_root, “open”)
self.my_tree.SetItemImage(status_node, self.folder_icon, wx.TreeItemIcon_Normal)

5.给节点绑定双击事件

self.my_tree.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.show_defect_info)
def show_defect_info(self, event):
    item = event.GetItem()
    self.my_tree.Expand(item)
    print(self.my_tree.GetItemText(item))

发表回复


苏ICP备18047533号-1