2013-03-20 76 views
0

我試圖學習wxpython和即時通訊從未編程我的生活之前。wxpython將文件夾添加到listctrl?

此應用程序與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() 
+0

你到底想幹什麼?當用戶選擇一個目錄時,是否要將它添加到ListCtrl的一列中?也許一個ListBox會更好。 – 2013-03-20 13:39:13

回答

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)