2013-08-28 30 views
1

如何動態地修改有關wx.FileDropTarget窗口的標題,如何動態地修改窗口的標題約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

您可以在父窗口傳遞到FileDropself.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() 
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() 
+0

想知道你是否意識到我在這之前一個小時發佈了答案? – pedram