2013-03-13 74 views
0

所以我想知道是否有人知道如何將文本從文本文件加載到框架上。 我的應用程序加載與符號匹配的鼓聲符和鼓聲音文件,並將它們加載在一起。現在我需要它加載一些文本來解釋符號,並且它應該將文本與圖像和聲音文件一起加載。WxPython加載文本

def loadImage(self, event): 
     self.image_file = self.images.next() 
     print(self.image_file) 
     image_file = os.path.join(IMAGE_DIR, self.image_file) 
     img = wx.Image(image_file, wx.BITMAP_TYPE_ANY) 
     width = img.GetWidth() 
     height = img.GetHeight() 
     self.imageCtrl.SetBitmap(wx.BitmapFromImage(img)) 

這是加載圖像的代碼。您也可以循環播放每個圖像和聲音文件。

無論如何,有沒有人有辦法添加到代碼來加載文本?

def loadImage(self, event): 
self.image_file = self.images.next() 
print(self.image_file) 
image_file = os.path.join(IMAGE_DIR, self.image_file) 
img = wx.Image(image_file, wx.BITMAP_TYPE_ANY) 
width = img.GetWidth() 
height = img.GetHeight() 
self.imageCtrl.SetBitmap(wx.BitmapFromImage(img)) 



def previousPicture(self, event): 
    self.image_file = self.images.prev() 
    print(self.image_file) 
    image_file = os.path.join(IMAGE_DIR, self.image_file) 
    img = wx.Image(image_file, wx.BITMAP_TYPE_ANY) 
    img = img.Scale(680,143) 
    self.imageCtrl.SetBitmap(wx.BitmapFromImage(img)) 

def onPlaySound (self, event): 
    sound_file, ext = os.path.splitext(self.image_file) 
    sound_file = os.path.join(SOUND_DIR, sound_file + '.wav') 
    print(sound_file) 
    sound = wx.Sound(sound_file) 
    sound.Play(wx.SOUND_ASYNC) 

class DIter: 
#"Iterable with next and previous" 
def __init__(self, ItemList): 
    #"Creator" 
    self.ItemList = ItemList 
    self.Index = -1 
    self.ListEnd = len(ItemList) 

def next(self): 
    # """ Return the next item """ 
    self.Index += 1 
    self.Index %= self.ListEnd # or to avoid wrapping self.Index = min([self.Index, self.ListEnd-1]) 
    return self.ItemList[self.Index] 

def prev(self): 
     #""" Return the previous item """ 
    self.Index -= 1 
    if self.Index < 0: 
     self.Index = self.ListEnd-1 # or to avoid wrapping self.Index = 0 
    return self.ItemList[self.Index] 

回答

1

添加一個wx.TextCtrl,可能帶有wx.TE_MULTILINE標誌並用文本更新它。您只需將文本讀入一個變量,然後通過SetValue()方法設置TextCtrl的值。

wxPython演示中有一些例子可以從wxPython網站下載。

這裏的排序是工作的例子:

import glob 
import wx 

######################################################################## 
class MainPanel(wx.Panel): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent) 
     self.textFiles = glob.glob(r'C:\Users\mdriscoll\Documents\wx_testing\*.txt') 
     self.currentFile = -1 
     self.totalFiles = len(self.textFiles) 

     mainSizer = wx.BoxSizer(wx.VERTICAL) 
     btnSizer = wx.BoxSizer(wx.HORIZONTAL) 

     self.txt = wx.TextCtrl(self, style=wx.TE_MULTILINE) 
     prevBtn = wx.Button(self, label="Prev") 
     prevBtn.Bind(wx.EVT_BUTTON, self.onPrev) 
     nextBtn = wx.Button(self, label="Next") 
     nextBtn.Bind(wx.EVT_BUTTON, self.onNext) 

     btnSizer.Add(prevBtn, 0, wx.ALL|wx.CENTER, 5) 
     btnSizer.Add(nextBtn, 0, wx.ALL|wx.CENTER, 5) 
     mainSizer.Add(self.txt, 1, wx.ALL|wx.EXPAND, 5) 
     mainSizer.Add(btnSizer) 

     self.SetSizer(mainSizer) 

    #---------------------------------------------------------------------- 
    def onPrev(self, event): 
     """""" 
     print self.currentFile 
     if self.currentFile != -1: 
      if self.currentFile == self.totalFiles: 
       # need to subtract one to get the last element in the list 
       self.currentFile -= 1 
       fpath = self.textFiles[self.currentFile] 
      else: 
       fpath = self.textFiles[self.currentFile] 

      # read file 
      with open(fpath) as fh: 
       data = fh.read() 

      self.txt.SetValue(data) 
      self.currentFile -= 1 
     else: 
      return 

    #---------------------------------------------------------------------- 
    def onNext(self, event): 
     """""" 
     print self.currentFile 
     if self.currentFile == -1: 
      self.currentFile += 1 

     if self.currentFile != self.totalFiles: 
      fpath = self.textFiles[self.currentFile] 
      # read file 
      with open(fpath) as fh: 
       data = fh.read() 

      self.txt.SetValue(data) 
      self.currentFile += 1 
     else: 
      return 



######################################################################## 
class MainFrame(wx.Frame): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     """Constructor""" 
     wx.Frame.__init__(self, None, title="File reader") 
     panel = MainPanel(self) 
     self.Show() 

if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MainFrame() 
    app.MainLoop() 

它應該給你的總體思路反正

+0

加載從在該wx.TextCtrl文本文件中的文本,我應該把它放在Def LoadImage載入圖像的地方。文本應該與符號一起加載解釋它,如果我正確地得到了我的觀點。 – Chudboy 2013-03-14 01:44:23

+0

是的,那很好。如果LoadImage調用需要很長時間,那麼您可能最終需要將加載內容放入一個線程中。 – 2013-03-14 13:39:09

+0

你有沒有可能給我一個我如何去做這個事情的例子?對不起,新手。 self.text_file = self.text.next() text_file = os.path.join(TEXT_DIR,self.text_file) txt = wx.TextCtrl(text_file) self.textCtrl (self.panel) 我沒有看到我可以使用的演示中的任何東西。 – Chudboy 2013-03-14 15:43:54