0

wxpython将文件夹添加到listctrl?

根据链接,我尝试制作一个将文件添加到 listctrl 的代码。但它没有用。

def OnButtonPath(self, event):  # wxGlade: MyDialog.<event_handler>
    dlg = wx.DirDialog(self, "Choose Directory...", \
                       style = wx.DD_DEFAULT_STYLE|wx.DD_DIR_MUST_EXIST)
    if dlg.ShowModal() == wx.ID_OK:
        self.listCtrl.Value = path = dlg.GetPath()
        #self.AddtoList(path)
    dlg.Destroy()

def AddToList(self, path):
    fname = os.path.basename(path)  # insert only folder-name
    self.listCtrlFile.Append(fname)

如果我在下面激活,我将无法打开 python GUI。

self.AddtoList(路径)

我想要的是将多个文件带入 listctrl。我怎样才能做到这一点?

================== 完整代码 ==================

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.6.8 (standalone edition) on Thu Jul 31 17:55:10 2014
#

import wx

# begin wxGlade: dependencies
import gettext
# end wxGlade

# begin wxGlade: extracode
# end wxGlade


class MyDialog(wx.Dialog):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyDialog.__init__
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.labelPath = wx.StaticText(self, wx.ID_ANY, _("Path : "))
        self.textCtrlPath = wx.TextCtrl(self, wx.ID_ANY, "")
        self.button_1 = wx.Button(self, wx.ID_ANY, _("Open..."))
        self.labelFile = wx.StaticText(self, wx.ID_ANY, _("File List :"))
        self.listCtrlFile = wx.ListCtrl(self, wx.ID_ANY, style=wx.LC_REPORT | wx.SUNKEN_BORDER)
        self.buttonAccept = wx.Button(self, wx.ID_ANY, _("RUN"))
        self.buttonExit = wx.Button(self, wx.ID_ANY, _("EXIT"))

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.OnButtonPath, self.button_1)
        self.Bind(wx.EVT_BUTTON, self.OnButtonAccept, self.buttonAccept)
        self.Bind(wx.EVT_BUTTON, self.OnButtonExit, self.buttonExit)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyDialog.__set_properties
        self.SetTitle(_("test"))
        self.textCtrlPath.SetBackgroundColour(wx.Colour(255, 255, 255))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyDialog.__do_layout
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        footerSizer = wx.BoxSizer(wx.HORIZONTAL)
        pathSizer = wx.BoxSizer(wx.HORIZONTAL)
        pathSizer.Add(self.labelPath, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 2)
        pathSizer.Add(self.textCtrlPath, 1, wx.LEFT | wx.RIGHT | wx.EXPAND, 3)
        pathSizer.Add(self.button_1, 0, 0, 0)
        mainSizer.Add(pathSizer, 0, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 4)
        mainSizer.Add(self.labelFile, 0, wx.ALL, 6)
        mainSizer.Add(self.listCtrlFile, 7, wx.ALL | wx.EXPAND, 4)
        footerSizer.Add(self.buttonAccept, 1, wx.LEFT | wx.RIGHT | wx.EXPAND, 3)
        footerSizer.Add(self.buttonExit, 1, wx.LEFT | wx.RIGHT | wx.EXPAND, 3)
        mainSizer.Add(footerSizer, 1, wx.ALL | wx.EXPAND | wx.ALIGN_RIGHT, 2)
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)
        self.Layout()
        # end wxGlade

    def OnButtonPath(self, event):  # wxGlade: MyDialog.<event_handler>
        dlg = wx.DirDialog(self, "Choose a directory:")
        if dlg.ShowModal() == wx.ID_OK:
            self.textCtrlPath.Value = dlg.GetPath()
        dlg.Destroy()

    def OnButtonAccept(self, event):  # wxGlade: MyDialog.<event_handler>
        print "Event handler 'OnButtonAccept' not implemented!"
        event.Skip()

    def OnButtonExit(self, event):  # wxGlade: MyDialog.<event_handler>
        self.Destroy()

# end of class MyDialog
if __name__ == "__main__":
    gettext.install("test") # replace with the appropriate catalog name

    test = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    mainDialog = MyDialog(None, wx.ID_ANY, "")
    test.SetTopWindow(mainDialog)
    mainDialog.Show()
    test.MainLoop()
4

1 回答 1

0

您可以通过使用 Python 的 glob 模块来获取通过DirDialog. 获得路径列表后,您可以遍历这些路径并使用列表控件的InsertStringItem方法将文件名添加到第一列。这是一个简单的例子:

import glob
import os
import wx

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        self.list_ctrl = wx.ListCtrl(self, 
                                     style=wx.LC_REPORT
                                     |wx.BORDER_SUNKEN
                                     )
        self.list_ctrl.InsertColumn(0, 'Filename')

        btn = wx.Button(self, label="Open Folder")
        btn.Bind(wx.EVT_BUTTON, self.onOpenDirectory)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 1, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onOpenDirectory(self, event):
        """"""
        dlg = wx.DirDialog(self, "Choose a directory:")
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self.updateDisplay(path)
        dlg.Destroy()

    #----------------------------------------------------------------------
    def updateDisplay(self, folder_path):
        """
        Update the listctrl with the file names in the passed in folder
        """
        paths = glob.glob(folder_path + "/*.*")
        for index, path in enumerate(paths):
            self.list_ctrl.InsertStringItem(index, os.path.basename(path))


########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Add Files")
        panel = MyPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

请注意,我使用 Python 的os模块仅获取文件名,而不是将整个路径插入到列表控件中。如果你想变得花哨,你可以添加额外的列来显示文件的大小、日期等,你可以从中得到os.stat

于 2014-07-31T13:38:49.603 回答