WxPython加载文本

时间:2013-03-13 19:05:21

标签: wxpython

所以我想知道是否有人知道如何将文本从文本文件加载到框架上。 我的应用程序加载与Notation匹配的Drum Notation和Drum声音文件,并将它们一起加载。现在我需要加载一些文本来解释符号,它应该将文本与图像和声音文件一起加载。

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 个答案:

答案 0 :(得分: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()

无论如何,它应该给你一般的想法