0

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

1 回答 1

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()
于 2013-09-23T17:33:05.803 回答