簡體   English   中英

wxpython添加文件夾到listctrl?

[英]wxpython add folder to listctrl?

我正在嘗試學習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()

假設您要將文件夾名稱插入到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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM