拖放文件后刷新ListCtrl

时间:2013-09-22 21:59:43

标签: drag-and-drop wxpython listctrl

我对wxpython比较陌生 - 非常感谢你能给我提供的任何帮助。基本上,我在关闭

之间的循环时遇到了麻烦

1)在我的OnDropFiles方法下填写一个名为ListOfFiles的列表 2)刷新FileList,使其显示ListOfFiles中的项目。

我知道如果你打电话

FileWindow(None, -1, 'List of Files and Actions')

就在OnDropFiles的末尾,它在一个新的框架中并在填充FileList listctrl时从ListOfFiles中绘制......但我希望有一种方法可以在同一个窗口中进行更新。我已经尝试使用Layout()并在FileWindowObject上调用各种方法......但是没有成功。

非常感谢你的帮助。我认为你给我的答案可能会导致我对wxpython的理解取得真正的突破。

#!/usr/bin/env python

import wx
import sys
import traceback
import time

APP_EXIT = 1
ListOfFiles = []


class FileDrop(wx.FileDropTarget):  #This is the file drop target
    def __init__(self, window):
        wx.FileDropTarget.__init__(self)  #File Drop targets are subsets of windows
        self.window = window

    def OnDropFiles(self, x, y, filenames):   #FileDropTarget now fills in the ListOfFiles

        for DragAndDropFile in filenames:
            ListOfFiles.append(DragAndDropFile)  #We simply append to the bottom of our list of files.  

class FileWindow(wx.Frame):

    def __init__(self, parent, id, title):  #This will initiate with an id and a title
        wx.Frame.__init__(self, parent, id, title, size=(300, 300))

        hbox = wx.BoxSizer(wx.HORIZONTAL)  #These are layout items
        panel = wx.Panel(self, -1)  #These are layout items

        self.FileList = wx.ListCtrl(panel, -1, style=wx.LC_REPORT)  #This builds the list control box

        DropTarget = FileDrop(self.FileList)  #Establish the listctrl as a drop target
        self.FileList.SetDropTarget(DropTarget)  #Make drop target.

        self.FileList.InsertColumn(0,'Filename',width=140)  #Here we build the columns

        for i in ListOfFiles:  #Fill up listctrl starting with list of working files
            InsertedItem = self.FileList.InsertStringItem(sys.maxint, i)  #Here we insert an item at the bottom of the list

        hbox.Add(self.FileList, 1, wx.EXPAND)
        panel.SetSizer(hbox)
        self.Show(True)

def main():
    ex = wx.App(redirect = True, filename = time.strftime("%Y%m%d%H%M%S.txt"))
    FileWindowObject = FileWindow(None, -1, 'List of Files and Actions')
    ex.MainLoop()

if __name__ == '__main__':
    main()  #Execute function#!/usr/bin/env python

1 个答案:

答案 0 :(得分:1)

问题是你所做的只是将项目添加到列表中,而不是添加到ListCtrl本身。您需要子类化wx.ListCtrl并添加某种更新方法。然后,您将调用该更新方法,而不是附加到您不在任何地方使用的列表。这是一种方法:

import wx
import time

########################################################################
class MyListCtrl(wx.ListCtrl):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.ListCtrl.__init__(self, parent, style=wx.LC_REPORT)
        self.index = 0

    #----------------------------------------------------------------------
    def dropUpdate(self, path):
        """"""
        self.InsertStringItem(self.index, path)
        self.index += 1

class FileDrop(wx.FileDropTarget):  #This is the file drop target
    def __init__(self, window):
        wx.FileDropTarget.__init__(self)  #File Drop targets are subsets of windows
        self.window = window

    def OnDropFiles(self, x, y, filenames):   #FileDropTarget now fills in the ListOfFiles

        for DragAndDropFile in filenames:
            self.window.dropUpdate(DragAndDropFile) # update list control


class FileWindow(wx.Frame):

    def __init__(self, parent, id, title):  #This will initiate with an id and a title
        wx.Frame.__init__(self, parent, id, title, size=(300, 300))

        hbox = wx.BoxSizer(wx.HORIZONTAL)  #These are layout items
        panel = wx.Panel(self, -1)  #These are layout items

        self.FileList = MyListCtrl(panel)  #This builds the list control box

        DropTarget = FileDrop(self.FileList)  #Establish the listctrl as a drop target
        self.FileList.SetDropTarget(DropTarget)  #Make drop target.

        self.FileList.InsertColumn(0,'Filename',width=140)  #Here we build the columns

        hbox.Add(self.FileList, 1, wx.EXPAND)
        panel.SetSizer(hbox)
        self.Show(True)

def main():
    ex = wx.App(redirect = True, filename = time.strftime("%Y%m%d%H%M%S.txt"))
    FileWindowObject = FileWindow(None, -1, 'List of Files and Actions')
    ex.MainLoop()

if __name__ == '__main__':
    main()
相关问题