wxPython:将文件拖到窗口中以获取文件路径

wat*_*ake 4 python wxwidgets wxpython drag-and-drop filepath

我想将文件拖到窗口中并获取文件路径.我试过这样做:

class CSVDropper(wx.FileDropTarget):
  def __init__(self, data):
      wx.FileDropTarget.__init__(self)
      self.data = data

  def OnDropFiles(self, x, y, filenames):
      self.data = filenames
      print self.data
Run Code Online (Sandbox Code Playgroud)

然后在主窗口中:

    # Drag & Drop
    self.csv_path = None
    self.drop_table = CSVDropper(self.csv_path)

    self.SetDropTarget(self.drop_table)
Run Code Online (Sandbox Code Playgroud)

但这没有任何作用.我已经尝试过运行这个教程代码,但它也没有做任何事情.我该如何做到这一点?

Mik*_*oll 10

打印时self.data,您应该看到打印出的路径列表.无论如何,我刚刚写了一篇关于drag-n-drop 的教程,展示了如何做到这一点.这是我的代码的略微修改版本,它既打印到stdout的文件路径也打印到文本控件:

import wx

########################################################################
class MyFileDropTarget(wx.FileDropTarget):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, window):
        """Constructor"""
        wx.FileDropTarget.__init__(self)
        self.window = window

    #----------------------------------------------------------------------
    def OnDropFiles(self, x, y, filenames):
        """
        When files are dropped, write where they were dropped and then
        the file paths themselves
        """
        self.window.SetInsertionPointEnd()
        self.window.updateText("\n%d file(s) dropped at %d,%d:\n" %
                              (len(filenames), x, y))
        print filenames
        for filepath in filenames:
            self.window.updateText(filepath + '\n')    

########################################################################
class DnDPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)

        file_drop_target = MyFileDropTarget(self)
        lbl = wx.StaticText(self, label="Drag some files here:")
        self.fileTextCtrl = wx.TextCtrl(self,
                                        style=wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY)
        self.fileTextCtrl.SetDropTarget(file_drop_target)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(lbl, 0, wx.ALL, 5)
        sizer.Add(self.fileTextCtrl, 1, wx.EXPAND|wx.ALL, 5)
        self.SetSizer(sizer)

    #----------------------------------------------------------------------
    def SetInsertionPointEnd(self):
        """
        Put insertion point at end of text control to prevent overwriting
        """
        self.fileTextCtrl.SetInsertionPointEnd()

    #----------------------------------------------------------------------
    def updateText(self, text):
        """
        Write text to the text control
        """
        self.fileTextCtrl.WriteText(text)

########################################################################
class DnDFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, parent=None, title="DnD Tutorial")
        panel = DnDPanel(self)
        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = DnDFrame()
    app.MainLoop()
Run Code Online (Sandbox Code Playgroud)