1

关于wx.FileDropTarget如何动态修改窗口标题,</p>

也就是说,当将文件拖放到窗口中时,窗口的标题会改变。

我知道SetTitle(os.path.basename(name)可以修改标题,但我不知道代码应该放在哪里。

帮帮我,谢谢!</p>

代码是:

import wx

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:
                file = open(name, 'r')
                text = file.read()
                self.window.WriteText(text)
                file.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, size = (450, 400))

        self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)
        dt = FileDrop(self.text)
        self.text.SetDropTarget(dt)
        self.Centre()
        self.Show(True)

app = wx.App()
DropFile(None, -1)
app.MainLoop()
4

2 回答 2

0

我知道了。

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()
于 2013-08-28T14:47:02.440 回答
0

您可以将父窗口类传递给FileDropas self.parent,并用于self.parent.SetTitle设置标题。这是您修改后的代码。我评论了我的更改。

import wx
import os # added this

class FileDrop(wx.FileDropTarget):
    def __init__(self, window, parent): # added parent attribute
        wx.FileDropTarget.__init__(self)
        self.window = window
        self.parent = parent # self.parent makes it available to the whole class

    def OnDropFiles(self, x, y, filenames):

        for name in filenames:
            try:
                file = open(name, 'r')
                text = file.read()
                self.window.WriteText(text)
                self.parent.SetTitle(os.path.basename(name)) # magic happens here
                file.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, size = (450, 400))

        self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)
        dt = FileDrop(self.text, self) # we say parent=self which let's the FileDrop class talk to the DropFile wx.Frame. 
        self.text.SetDropTarget(dt)
        self.Centre()
        self.Show(True)

app = wx.App()
DropFile(None, -1)
app.MainLoop()
于 2013-08-28T14:01:22.223 回答