如何从tree目录获取文件路径以使用wxpython读取文本文件行..?

问题描述 投票:0回答:1
#!/usr    /bin/python
# -*- coding: utf-8 -*-
import wx
import os    

class RandomObj(object):

    def __init__(self, name):
        self.name = name
class TreeExample(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Database', size=(200, 130))
        self.tree = wx.TreeCtrl(self, size=(200, 100))
        root = self.tree.AddRoot('Database')
        self.tree.AppendItem(root, 'Class A')
        files = []
        for (dirname, dirnames, filenames) in \
            os.walk('D:/Database/Class A'):

            # print path to all subdirectories first.

            for subdirname in dirnames:
                files.append(os.path.join(dirname, subdirname))
                # print path to all filenames.
                for filename in filenames:
                    self.item = files.append(os.path.join(dirname,
                            filename))
                    self.tree.AppendItem(self.tree.GetLastChild(root),
                            filename)
        self.sel = []
        for (i, j) in enumerate(files):
            if j.endswith('.txt'):
                self.sel.append(j)
        print self.sel
        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated,
                  self.tree)
    def OnActivated(self, event):
        value = event.GetItem()
        f = open(value, 'r')
        lines = f.readlines()
        print lines[0]
        print lines[1]
        print 'Double clicked on', self.tree.GetItemText(value)

我试图传递给open()的'value'是treeitemid,它需要filepath

tree directory wxpython traversal
1个回答
0
投票

我想你必须走回树上。 这似乎有效,尽管您可能想等待更好的答案

#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
import os

class RandomObj(object):

    def __init__(self, name):
        self.name = name

class TreeExample(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Database', size=(200, 130))
        self.tree = wx.TreeCtrl(self, size=(200, 100))
        root = self.tree.AddRoot('/home/public')
        self.root = '/home/public'
        self.tree.AppendItem(root, 'Music')
        files = []
        for (dirname, dirnames, filenames) in \
            os.walk('/home/public/Music'):

            # print path to all subdirectories first.

            for subdirname in dirnames:
                files.append(os.path.join(dirname, subdirname))
                # print path to all filenames.
                for filename in filenames:
                    self.item = files.append(os.path.join(dirname,
                            filename))
                    self.tree.AppendItem(self.tree.GetLastChild(root),
                            filename)
        self.sel = []
        for (i, j) in enumerate(files):
            if j.endswith('.txt'):
                self.sel.append(j)
        #print self.sel
        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated,
                  self.tree)

    def OnActivated(self, event):
        value = self.tree.GetSelection()
        fname = []
        while self.tree.GetItemParent(value):
            fname.insert(0,"/"+self.tree.GetItemText(value))
            value = self.tree.GetItemParent(value)
        fname.insert(0,self.root)
        the_file = "".join(fname)
        f = open(the_file, 'r')
        lines = f.readlines()
        print (lines[0])
        print (lines[1])
        print ('Double clicked on', str(the_file))
if __name__ == '__main__':
    app = wx.App()
    frame = TreeExample()
    frame.Show()
    app.MainLoop()
© www.soinside.com 2019 - 2024. All rights reserved.