wxpython TreeCtrl with os.listdir

Discussion in 'Python' started by vedrandekovic@v-programs.com, Aug 3, 2007.

  1. Guest

    Hello,

    Does anybody know how can I "insert" os.listdir items in wx python
    TreeCtrl and every item assign adequately
    icon on this example
    import wx


    class TestFrame(wx.Frame):
    def __init__(self):
    wx.Frame.__init__(self, None, title="simple tree with icons",
    size=(400,500))


    il = wx.ImageList(16,16)

    # adequately icons
    self.fldridx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER,
    wx.ART_OTHER, (16,16))) # icon for os.listdir folder
    self.fldropenidx =
    il.Add(wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN,wx.ART_OTHER,
    (16,16)))
    # icon for os.listdir file
    self.fileidx =
    il.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER,
    (16,16)))
    # icon for os.listdir open folder



    self.tree = wx.TreeCtrl(self)

    self.tree.AssignImageList(il)
    root = self.tree.AddRoot("wx.Object")
    self.tree.SetItemImage(root,
    self.fldridx,wx.TreeItemIcon_Normal)
    self.tree.SetItemImage(root,
    self.fldropenidx,wx.TreeItemIcon_Expanded)

    self.AddTreeNodes(root, data.tree) # There must be os.listdir
    items
    self.tree.Expand(root)


    def AddTreeNodes(self, parentItem, items):
    for item in items:
    if type(item) == str:
    newItem = self.tree.AppendItem(parentItem, item)
    self.tree.SetItemImage(newItem,
    self.fileidx,wx.TreeItemIcon_Normal)
    else:
    newItem = self.tree.AppendItem(parentItem, item[0])
    self.tree.SetItemImage(newItem,
    self.fldridx,wx.TreeItemIcon_Normal)
    self.tree.SetItemImage(newItem,
    self.fldropenidx,wx.TreeItemIcon_Expanded)

    self.AddTreeNodes(newItem, item[1])


    def GetItemText(self, item):
    if item:
    return self.tree.GetItemText(item)
    else:
    return ""

    app = wx.PySimpleApp(redirect=True)
    frame = TestFrame()
    frame.Show()
    app.MainLoop()



    Regards,
    Vedran
     
    , Aug 3, 2007
    #1
    1. Advertising

  2. Guest

    On Aug 3, 6:56 am, wrote:
    > Hello,
    >
    > Does anybody know how can I "insert" os.listdir items in wx python
    > TreeCtrl and every item assign adequately
    > icon on this example
    > import wx
    >
    > class TestFrame(wx.Frame):
    > def __init__(self):
    > wx.Frame.__init__(self, None, title="simple tree with icons",
    > size=(400,500))
    >
    > il = wx.ImageList(16,16)
    >
    > # adequately icons
    > self.fldridx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER,
    > wx.ART_OTHER, (16,16))) # icon for os.listdir folder
    > self.fldropenidx =
    > il.Add(wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN,wx.ART_OTHER,
    > (16,16)))
    > # icon for os.listdir file
    > self.fileidx =
    > il.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER,
    > (16,16)))
    > # icon for os.listdir open folder
    >
    > self.tree = wx.TreeCtrl(self)
    >
    > self.tree.AssignImageList(il)
    > root = self.tree.AddRoot("wx.Object")
    > self.tree.SetItemImage(root,
    > self.fldridx,wx.TreeItemIcon_Normal)
    > self.tree.SetItemImage(root,
    > self.fldropenidx,wx.TreeItemIcon_Expanded)
    >
    > self.AddTreeNodes(root, data.tree) # There must be os.listdir
    > items
    > self.tree.Expand(root)
    >
    > def AddTreeNodes(self, parentItem, items):
    > for item in items:
    > if type(item) == str:
    > newItem = self.tree.AppendItem(parentItem, item)
    > self.tree.SetItemImage(newItem,
    > self.fileidx,wx.TreeItemIcon_Normal)
    > else:
    > newItem = self.tree.AppendItem(parentItem, item[0])
    > self.tree.SetItemImage(newItem,
    > self.fldridx,wx.TreeItemIcon_Normal)
    > self.tree.SetItemImage(newItem,
    > self.fldropenidx,wx.TreeItemIcon_Expanded)
    >
    > self.AddTreeNodes(newItem, item[1])
    >
    > def GetItemText(self, item):
    > if item:
    > return self.tree.GetItemText(item)
    > else:
    > return ""
    >
    > app = wx.PySimpleApp(redirect=True)
    > frame = TestFrame()
    > frame.Show()
    > app.MainLoop()
    >
    > Regards,
    > Vedran


    This looks like something to post to the wxPython user's group, found
    here: http://www.wxpython.org/maillist.php

    The treectrl is one of the more complicated widgets of the wxPython
    set.

    Mike
     
    , Aug 3, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kai
    Replies:
    1
    Views:
    366
  2. kai
    Replies:
    0
    Views:
    365
  3. kai
    Replies:
    0
    Views:
    313
  4. kai
    Replies:
    0
    Views:
    308
  5. Saketh
    Replies:
    1
    Views:
    524
    jean-michel bain-cornu
    Jun 12, 2006
Loading...

Share This Page