免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5424 | 回复: 3
打印 上一主题 下一主题

(请教)wxpython 关于文件拖放,类参数的传入与方法调用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-08-28 14:06 |只看该作者 |倒序浏览
本帖最后由 avyou 于 2013-08-28 14:08 编辑

这个一个文件拖放到 TextCtrl 文本框的代码,我想当文件拖放到文本框时,主框架窗口的标题发生改变(变放拖入文件的文件),
但我不知道 class DropFile(wx.Frame) 中的 dt = FileDrop(self.text,此框架) 如何传入给 FileDrop(wx.FileDropTarget),让它可以调用

self.frame.SetTitle("aaaaa") 修改主窗口标题。麻烦各位大侠帮帮忙,在此谢过了!
  1. #!/usr/bin/python

  2. import wx

  3. class FileDrop(wx.FileDropTarget):
  4.     def __init__(self, window,frame):
  5.         wx.FileDropTarget.__init__(self)
  6.         self.window = window
  7.         self.frame = frame
  8.         
  9.     def OnDropFiles(self, x, y, filenames):

  10.         for name in filenames:
  11.             try:
  12.                 file = open(name, 'r')
  13.                 text = file.read()
  14.                 self.window.WriteText(text)
  15.                 self.frame.SetTitle("aaaaa")
  16.                 file.close()
  17.             except IOError, error:
  18.                 dlg = wx.MessageDialog(None, 'Error opening file\n' + str(error))
  19.                 dlg.ShowModal()
  20.             except UnicodeDecodeError, error:
  21.                 dlg = wx.MessageDialog(None, 'Cannot open non ascii files\n' + str(error))
  22.                 dlg.ShowModal()

  23. class DropFile(wx.Frame):
  24.     def __init__(self, parent, id):
  25.         wx.Frame.__init__(self, parent, id, size = (450, 400))
  26.         self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)
  27.         
  28.         dt = FileDrop(self.text,此框架)
  29.         self.text.SetDropTarget(dt)
  30.         self.Centre()
  31.         self.Show(True)

  32. app = wx.App()
  33. DropFile(None, -1)
  34. app.MainLoop()
复制代码

论坛徽章:
0
2 [报告]
发表于 2013-08-28 21:02 |只看该作者
本帖最后由 avyou 于 2013-08-28 21:02 编辑

已搞定,



  1.     import wx,os
  2.     class FileDrop(wx.FileDropTarget):
  3.         def __init__(self,window,frame):
  4.             wx.FileDropTarget.__init__(self)
  5.             self.window = window
  6.             self.frame = frame
  7.         def OnDropFiles(self, x, y, filenames):
  8.             for name in filenames:
  9.                 try:
  10.                     f = open(name, 'r')
  11.                     text = f.read()
  12.                     self.window.WriteText(text)
  13.                     self.frame.SetTitle(os.path.basename(name))
  14.                     f.close()
  15.                 except IOError, error:
  16.                     dlg = wx.MessageDialog(None, 'Error opening file\n' + str(error))
  17.                     dlg.ShowModal()
  18.                 except UnicodeDecodeError, error:
  19.                     dlg = wx.MessageDialog(None, 'Cannot open non ascii files\n' + str(error))
  20.                     dlg.ShowModal()
  21.   

  22.     class DropFile(wx.Frame):
  23.         def __init__(self, parent, id):
  24.             wx.Frame.__init__(self, parent, id, title="", size = (450, 400))

  25.             self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE|wx.TE_RICH2)
  26.             dt = FileDrop(self.text,self)
  27.             self.text.SetDropTarget(dt)

  28.             self.Centre()
  29.             self.Show(True)

  30.     app = wx.App()
  31.     DropFile(None, -1)
  32.     app.MainLoop()
复制代码

论坛徽章:
3
卯兔
日期:2013-08-15 13:17:31处女座
日期:2014-01-10 11:35:23双子座
日期:2014-01-25 02:16:06
3 [报告]
发表于 2013-08-29 12:26 |只看该作者
好人啊,弄好了还不忘上传结果!

论坛徽章:
0
4 [报告]
发表于 2013-08-29 12:40 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP