如何动态修改关于wx.FileDropTarget的窗口标题,
也就是说,当将文件拖放到窗口中时,窗口的标题会被更改。
我知道SetTitle(os.path.basename(name)
可以修改标题,但我不知道代码应该放在哪里。
帮帮我,谢谢!
代码是:
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()
答案 0 :(得分:0)
您可以将父窗口类作为FileDrop
传递给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()
答案 1 :(得分: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()