【问题标题】:refresh ListCtrl after drag and drop files拖放文件后刷新 ListCtrl
【发布时间】:2013-09-22 21:59:42
【问题描述】:

我对 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

【问题讨论】:

    标签: drag-and-drop wxpython listctrl


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 2011-06-25
      • 1970-01-01
      • 2011-06-03
      • 2020-08-08
      • 1970-01-01
      • 2013-10-21
      相关资源
      最近更新 更多