【问题标题】:Retrieve a value from a different class从不同的类中检索值
【发布时间】:2020-09-24 12:10:36
【问题描述】:

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

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

【问题讨论】:

    标签: python oop wxpython


    【解决方案1】:

    主要问题是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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 2017-12-18
      相关资源
      最近更新 更多