wxpython将文件夹添加到listctrl?

时间:2013-03-20 04:48:11

标签: python wxpython

我正在努力学习wxpython,而且我从未在我的生活中编程。

此应用程序使用listctrl和dirdialog。 但后来我想在listctrl中添加文件夹(或者是我应该使用的listctrl?)

我该怎么做?

或者我做错了吗?

无论如何,这是代码。

# -*- coding: cp1252 -*-
import os
import wx


# Menu ID
ID_QUIT=1
ID_ABOUT=2
ID_ADD=3

class Frame(wx.Frame):
def __init__(self, parent, id, title,):
    wx.Frame.__init__(self, parent,id , title, size=(700, 750),)
    self.Center()
    self.Show()
    self.CreateStatusBar()
    self.SetStatusText("Status bar") #Statusbar in the bottom of the window
    panel = wx.Panel(self, id)

    #Listctrl
    self.index = 0

    self.list_ctrl = wx.ListCtrl(panel, size=(400,600),
                                 style=wx.LC_REPORT
                                 |wx.SUNKEN_BORDER
                                 )
    self.list_ctrl.InsertColumn(2, 'Name',width = 401)


    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(self.list_ctrl, 0, wx.ALL|wx.TOP, 30)
    panel.SetSizer(sizer)


            #Menu
    menuBar = wx.MenuBar()
    filemenu = wx.Menu()
    helpmenu = wx.Menu()
    filemenu.Append(ID_ADD,"&Add\tCtrl+A", "Adding directory")
    filemenu.Append(ID_QUIT, "&Quit\tCtrl+Q", "Quit application")
    helpmenu.Append(ID_ABOUT, "&About\tCtrl+A", "About application")
    menuBar.Append(filemenu, "&File")
    menuBar.Append(helpmenu, "&Help")
    self.SetMenuBar(menuBar)

    #Event
    wx.EVT_MENU(self, ID_ADD, self.onDir)
    self.Bind(wx.EVT_MENU, self.QuitWindow, id=ID_QUIT)
    self.Bind(wx.EVT_MENU, self.OnAboutBox, id=ID_ABOUT)  

                    # DirDialog
    self.currentDirectory = os.getcwd()

    dirDlgBtn = wx.Button(panel, label="Add", pos=(600, 10), size=(60, 30))
    dirDlgBtn.Bind(wx.EVT_BUTTON, self.onDir)


def onDir(self, event):
    """
    Show the DirDialog and print the user's choice to stdout
    """
    dlg = wx.DirDialog(self, "Choose a directory:",
                       style=wx.DD_DEFAULT_STYLE
                       #| wx.DD_DIR_MUST_EXIST
                       #| wx.DD_CHANGE_DIR
                       )
    if dlg.ShowModal() == wx.ID_OK:
        print "You chose %s" % dlg.GetPath()
    dlg.Destroy()
                    # END

def QuitWindow(self, event):
    self.Destroy()

def OnAboutBox(self, event):
    description = "text"
    licence = "text"
    info = wx.AboutDialogInfo()
    info.SetName ('text')
    info.SetVersion ('0.1')
    info.SetDescription(description)
    info.SetCopyright ('text')
    info.SetLicence(licence)  

    wx.AboutBox(info)


class App(wx.App):
def OnInit(self):
    frame = Frame(None, -1, title = "Film")
    frame.Show(1+1==2)
    self.SetTopWindow(frame)
    return True

app = App(0)
app.MainLoop()

1 个答案:

答案 0 :(得分:0)

假设您要将文件夹名称插入listctrl,一个干净的方法是编写一个小函数并从onDir函数调用它:

def onDir(self, event):
    """
    Show the DirDialog and print the user's choice to stdout
    """
    dlg = wx.DirDialog(self, "Choose a directory:",
                   style=wx.DD_DEFAULT_STYLE
                   #| wx.DD_DIR_MUST_EXIST
                   #| wx.DD_CHANGE_DIR
                   )
    if dlg.ShowModal() == wx.ID_OK:
        path = dlg.GetPath()
        print "You chose %s" % path
        self.AddToList(path)
    dlg.Destroy()

def AddToList(self, path):
    fname = os.path.basename(path)  # insert only folder-name
    self.list_ctrl.Append(fname)
相关问题