wxpython核心部件wx.treectrl用法示例

发布时间:2020-06-08编辑:脚本学堂
有关wxpython中核心部件wx.treectrl的用法,wx.treectrl 用来以层次关系展示数据,wxpython中文教程实例,需要的朋友参考下。

在wxpython中,wx.TreeCtrl 用来以层次关系展示数据。
 
>> 专题:wxpython中文教程

例子:
 

复制代码 代码示例:

#!/usr/bin/python
#coding=utf-8
#treectrl.py

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title,
                          wx.DefaultPosition, wx.Size(450, 350))

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        vbox = wx.BoxSizer(wx.VERTICAL)
        panel1 = wx.Panel(self, -1)
        panel2 = wx.Panel(self, -1)
       
        self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1, -1),
                                wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)
        root = self.tree.AddRoot('程序员')
        os = self.tree.AppendItem(root, '操作系统')
        pl = self.tree.AppendItem(root, '编程语言')
        tk = self.tree.AppendItem(root, '工具套件')
        self.tree.AppendItem(os, 'linux')
        self.tree.AppendItem(os, 'FreeBSD')
        self.tree.AppendItem(os, 'OpenBSD')
        self.tree.AppendItem(os, 'NetBSD')
        self.tree.AppendItem(os, 'Solaris')
        cl = self.tree.AppendItem(pl, '编译语言')
        sl = self.tree.AppendItem(pl, '脚本语言')
        self.tree.AppendItem(cl, 'Java')
        self.tree.AppendItem(cl, 'C++')
        self.tree.AppendItem(cl, 'C')
        self.tree.AppendItem(cl, 'Pascal')
        self.tree.AppendItem(sl, 'Ruby')
        self.tree.AppendItem(sl, 'Tcl')
        self.tree.AppendItem(sl, 'PHP')
        self.tree.AppendItem(sl, 'Python')
        self.tree.AppendItem(tk, 'Qt')
        self.tree.AppendItem(tk, 'MFC')
        self.tree.AppendItem(tk, 'wxPython')
        self.tree.AppendItem(tk, 'GTK+')
        self.tree.AppendItem(tk, 'Swing')
        self.tree.Bind(wx.EVT_TREE_SEL_CHANGED,
                       self.OnSelChanged, id=1)
        self.display = wx.StaticText(panel2, -1, '',
                                     (10, 10), style=wx.ALIGN_CENTER)
        vbox.Add(self.tree, 1, wx.EXPAND)
        hbox.Add(panel1, 1, wx.EXPAND)
        hbox.Add(panel2, 1, wx.EXPAND)

        panel1.SetSizer(vbox)
        self.SetSizer(hbox)
        self.Center()
       
    def OnSelChanged(self, event):
        item = event.GetItem()
        self.display.SetLabel(self.tree.GetItemText(item))     
     
class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'treectrl.py')
        frame.Show(True)
        self.SetTopWindow(frame)
        return True
   
app = MyApp(0)
app.MainLoop()

首先,创建一个根项目:
root = self.tree.AddRoot('程序员')

在本例中,根项目不会显示出来,因为在构建器中用了 wx.TR_HIDE_ROOT 标志.
 

self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1, -1),
         wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)

通过 AppendItem() 方法将项目添加进根项目.
 

os = self.tree.AppendItem(root, '操作系统')
pl = self.tree.AppendItem(root, '编程语言')
tk = self.tree.AppendItem(root, '工具套件')

采用类似方式,我们可以简单地将项目添加进已有的项目中,从而创建出不同的级别.
运用 wx.EVT_TREE_SEL_CHANGED 事件处理器来捕获事件.
wx.EVT_TREE_SEL_CHANGED(self.tree, 1, self.OnSelChanged)

一些 wx.TreeCtrl 的构建器样式标志:
 

TR_NO_BUTTONS
TR_HAS_BUTTONS
TR_NO_LINES
TR_LINES_AT_ROOT
TR_SINGLE
TR_MULIPLE
TR_EXTENDED
TR_HAS_VARIABLE_ROW_HEIGHT
TR_EDIT_LABLES
TR_HIDE_ROOT
TR_ROW_LINES
TR_FULL_ROW_HIGHLIGHT
TR_DEFAULT_STYLE
TR_TWIST_BUTTONS
TR_MAC_BUTTONS
TR_AQUA_BUTTONS

如图:

<a href=http://www.jb200.com/python/wxpythonhexinbujian/ target=_blank class=infotextkey>wxpython核心部件</a>wx.treectrl