Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从其他类检索值_Python_Oop_Wxpython - Fatal编程技术网

Python 从其他类检索值

Python 从其他类检索值,python,oop,wxpython,Python,Oop,Wxpython,我在尝试检索程序拖放区中的值时遇到问题。 我真的很抱歉,我是Python新手,也是OOP新手,所以如果我的问题有点愚蠢,我很抱歉 我的目标是在DnDPanel类中单击按钮(btn)后检索放置在拖放区域中的文件名。 我尝试了很多方法,但仍然无法从“OnDropFiles”方法中获取“filename”值,我创建了“buff_pdf”方法来实现这一点,但它不起作用。 我一定是做错了什么,但我不知道是什么。 谢谢你的帮助 import wx #############################

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

我的目标是在DnDPanel类中单击按钮(btn)后检索放置在拖放区域中的文件名。 我尝试了很多方法,但仍然无法从“OnDropFiles”方法中获取“filename”值,我创建了“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
类的属性,以便从父级进行访问

以下是工作代码:

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()