如何解决从其他类别中检索值
我在尝试在程序的拖放区中检索值时遇到麻烦。 真的很抱歉,我是Python和OOP的新手,所以对不起,如果我的问题有点傻。
我的目标是在DnDPanel类中单击按钮(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),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,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),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,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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。