wxpython 请问如何将拖放文件名修改到窗口标题?

avyou 发布于 2013/08/27 22:56
阅读 639
收藏 0

原代码如下: 

import wx,os
class FileDrop(wx.FileDropTarget):
    def __init__(self, window):
        wx.FileDropTarget.__init__(self)
        self.window = window

    def OnDropFiles(self, x, y, filenames):
        for name in filenames:
            try:
                f = open(name, 'r')
                text = f.read()
                self.window.WriteText(text)
                f.close()
            except IOError, error:
                dlg = wx.MessageDialog(None, 'Error opening file\n' + str(error))
                dlg.ShowModal()
            except UnicodeDecodeError, error:
                dlg = wx.MessageDialog(None, 'Cannot open non ascii files\n' + str(error))
                dlg.ShowModal()
class DropFile(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, title="", size = (450, 400))

        self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE|wx.TE_RICH2)
        dt = FileDrop(self.text)
        self.text.SetDropTarget(dt)
        self.Centre()
        self.Show(True)
app = wx.App()
DropFile(None, -1)
app.MainLoop()

我想根据拖放的文件,将文件名动态更新为窗口的标题,如: 


我知道可以通过

SetTitle(os.path.basename(name))
来设置窗口标题,但我不知道如何在类中调用或使用?求指点!谢谢!
加载中
0
a
avyou

已搞定:

    import wx,os
    class FileDrop(wx.FileDropTarget):
        def __init__(self,window,frame):
            wx.FileDropTarget.__init__(self)
            self.window = window
            self.frame = frame
        def OnDropFiles(self, x, y, filenames):
            for name in filenames:
                try:
                    f = open(name, 'r')
                    text = f.read()
                    self.window.WriteText(text)
                    self.frame.SetTitle(os.path.basename(name))
                    f.close()
                except IOError, error:
                    dlg = wx.MessageDialog(None, 'Error opening file\n' + str(error))
                    dlg.ShowModal()
                except UnicodeDecodeError, error:
                    dlg = wx.MessageDialog(None, 'Cannot open non ascii files\n' + str(error))
                    dlg.ShowModal()

    class DropFile(wx.Frame):
        def __init__(self, parent, id):
            wx.Frame.__init__(self, parent, id, title="", size = (450, 400))
            self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE|wx.TE_RICH2)
            dt = FileDrop(self.text,self)
            self.text.SetDropTarget(dt)

            self.Centre()
            self.Show(True)

    app = wx.App()
    DropFile(None, -1)
    app.MainLoop()
0
h
hanwei6859506
我试过了。不行。窗口根本不响应。
0
h
hanwei6859506
你可以加我QQ,286142417.咱俩是否能讨论以下。
0
a
asdfasfaf

(1)33行 增加一个名字 frame = DropFile(None,-1)
(2)13行 frame.SetTitle(os.path.basename(name))

你试试


返回顶部
顶部