从不同的 class 中检索值

Retrieve a value from a different class

我在尝试检索程序拖放区中的值时遇到问题。 真的很抱歉,我是 Python 的新手,也是 OOP 的新手,所以如果我的问题有点愚蠢,我很抱歉。

我的 objective 是在单击 DnDPanel class 中的按钮 (btn) 后检索放置在拖放区中的文件名。 我尝试了很多方法,但仍然无法从 'OnDropFiles' 方法访问 'filenames' 值,我创建了 'buff_pdf' 方法来这样做,但它不起作用。 我一定做错了什么,但我不知道是什么。 感谢您的帮助!

import wx

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

    #----------------------------------------------------------------------
    def __init__(self, window):
        """Constructor"""
        wx.FileDropTarget.__init__(self)
        self.window = window
       
    #----------------------------------------------------------------------
    def OnDropFiles(self, x, y, filenames):
        """
        Quand les fichiers sont glissés, écrit le chemin depuis lequel
        ils viennent
        """
        self.window.SetInsertionPointEnd()
        self.window.updateText("\n%d fichier reçu %d,%d:\n" %
                              (len(filenames), x, y))
        for filepath in filenames:
            self.window.updateText(filepath + '\n')

        return True
        
########################################################################
class DnDPanel(wx.Panel, MyFileDropTarget):

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)
        file_drop_target = MyFileDropTarget(self)
        lbl = wx.StaticText(self, label="Put your PDF file in the drop zone :")
        self.fileTextCtrl = wx.TextCtrl(self,
                                        style=wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY)
        self.fileTextCtrl.SetDropTarget(file_drop_target)
        btn = wx.Button(self, label='buff files')
        #Retrieve filenames onclick
        btn.Bind(wx.EVT_BUTTON, self.buff_pdf)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(lbl, 0, wx.ALL, 25)
        sizer.Add(self.fileTextCtrl, 1, wx.EXPAND|wx.ALL, 5)
        self.SetSizer(sizer)

#----------------------------------------------------------------------

    def buff_pdf(self, event, MyFileDropTarget):
        """
        Retrieve filenames after clicking on the button (btn)
        """
        MyFileDropTarget.OnDropFiles(self)
        obj = MyFileDropTarget(self)
        obj.OnDropFiles(self)
        print(self.filenames)
        
    #----------------------------------------------------------------------
    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="PDF Buffer")
        panel = DnDPanel(self)
        self.Show()

########################################################################

if __name__ == "__main__":
    app = wx.App(False)
    frame = DnDFrame()
    app.MainLoop()

主要问题是 DnDPanel 继承自 MyFileDropTarget 并且还创建了 MyFileDropTarget 的子实例。最好只创建子实例并访问它。

filenames 变量也需要定义为 MyFileDropTarget class 的属性,以便从父级访问。

这是工作代码:

import wx

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

    #----------------------------------------------------------------------
    def __init__(self, window):
        """Constructor"""
        wx.FileDropTarget.__init__(self)
        self.window = window
        self.filenames = []   # create attribute for later reference
       
    #----------------------------------------------------------------------
    def OnDropFiles(self, x, y, filenames):
        """
        Quand les fichiers sont glissés, écrit le chemin depuis lequel
        ils viennent
        """
        self.window.SetInsertionPointEnd()
        self.window.updateText("\n%d fichier reçu %d,%d:\n" %
                              (len(filenames), x, y))
        for filepath in filenames:
            self.window.updateText(filepath + '\n')
        
        self.filenames.extend(filenames)   # update attribute

        return True
        
########################################################################
class DnDPanel(wx.Panel):

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)
        self.file_drop_target = MyFileDropTarget(self)  # create child object
        lbl = wx.StaticText(self, label="Put your PDF file in the drop zone :")
        self.fileTextCtrl = wx.TextCtrl(self,
                                        style=wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY)
        self.fileTextCtrl.SetDropTarget(self.file_drop_target)
        btn = wx.Button(self, label='buff files')
        #Retrieve filenames onclick
        btn.Bind(wx.EVT_BUTTON, self.buff_pdf)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(lbl, 0, wx.ALL, 25)
        sizer.Add(self.fileTextCtrl, 1, wx.EXPAND|wx.ALL, 5)
        self.SetSizer(sizer)

#----------------------------------------------------------------------

    def buff_pdf(self, event):
        """
        Retrieve filenames after clicking on the button (btn)
        """
        # MyFileDropTarget.OnDropFiles(self)
        # obj = MyFileDropTarget(self)
        # obj.OnDropFiles(self)
        print(self.file_drop_target.filenames)  # from child object
        
    #----------------------------------------------------------------------
    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="PDF Buffer")
        panel = DnDPanel(self)
        self.Show()

########################################################################

if __name__ == "__main__":
    app = wx.App(False)
    frame = DnDFrame()
    app.MainLoop()