Loading Text Using TextCtrl.

Hello,

So here is the basics of my question http://stackoverflow.com/questions/15394229/wxpython-loading-text/15395331?noredirect=1#comment21800452_15395331

Now Mike Driscoll has given a solution, however I was wondering if anyone had a solution for this to be loaded in the same way as the Image and sound file is being loaded.

For example this is how the Image and sound file are loaded together -

self.images = DIter([filename for filename in os.listdir(IMAGE_DIR) if any(filename.lower().endswith(ext) for ext in (’.png’))])

self.img = wx.Image(‘Background.png’)

self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.BitmapFromImage(self.img), size=(680,143), pos=(0, 0))

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)

I use an additional class to create a list and the “Next” and “Previous” functions -

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.Index = 0 #self.ListEnd-1 # or to avoid wrapping 

return self.ItemList[self.Index]

Now I would of thought I could of created the text to be loaded in the same manner using something like this -

self.text = DIter([filename for filename in os.listdir(TEXT_DIR) if any(filename.lower().endswith(ext) for ext in (’.txt’))])

self.txt = wx.TextCtrl(self, style=wx.TE_MULTILINE, pos=(150,300), size=(680,200))

def loadImage(self, event):

self.text_file = self.text.next()

print(self.image_text)

text_file = os.path.join(TEXT_DIR, self.tex_file)

self.TextCtrl

Though I’m not sure where to go from there so that the text is loaded onto a field on the form.

I do appreciate Mike’s solution, I just thought there would of been a similar way to the other Load function.

Thanks

Sion Jones

(Sorry to post here regularly)

Sion Jones wrote:

Though I'm not sure where to go from there so that the text is loaded
onto a field on the form.

You need to open the file -
fh = open(text_file)
and then set the value of the TextCtrl -
self.txt.SetValue(fh.read())

Alternatively, you can use the LoadFile method provided by TextCtrl -
self.txt.LoadFile(text_file)

You might also want to set the wx.TE_READONLY style on your TextCtrl if
you don't want your users to edit the data.

···

--
James Scholes
http://twitter.com/JamesScholes

So something like this?

def loadImage(self, event):

self.text_file = self.text.next()

print(self.image_text)

text_file = os.path.join(TEXT_DIR, self.tex_file)

self.TextCtrl

fh = open(text_file)
self.txt.SetValue(fh.read())
···

On Friday, March 15, 2013 2:41:21 AM UTC, James Scholes wrote:

Sion Jones wrote:

Though I’m not sure where to go from there so that the text is loaded

onto a field on the form.

You need to open the file -

fh = open(text_file)

and then set the value of the TextCtrl -

self.txt.SetValue(fh.read())

Alternatively, you can use the LoadFile method provided by TextCtrl -

self.txt.LoadFile(text_file)

You might also want to set the wx.TE_READONLY style on your TextCtrl if

you don’t want your users to edit the data.


James Scholes

http://twitter.com/JamesScholes

Yes, but without the typos.
def loadTextFile(self, event):
self.text_file = self.text.next()
path = os.path.join(TEXT_DIR, self.text_file)
self.txt.SetValue( open(path).read() )

···

Sion Jones wrote:

So something like this?

def loadImage(self, event):

                self.text_file

= self.text.next()

print(self.image_text)

                text_file =

os.path.join(TEXT_DIR, self.tex_file)

self.TextCtrl
fh = open(text_file)

self.txt.SetValue(fh.read())

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

Thanks Tim! Much Appreciated.

Though I ended up going with this

self.txt.LoadFile(os.path.join(TEXT_DIR, self.text.next()))

As Yoriz recommended, and it works perfectly! But thanks for the additional help!

···

On Friday, March 15, 2013 5:02:14 PM UTC, Tim Roberts wrote:

Sion Jones wrote:

So something like this?

def loadImage(self, event):

                self.text_file

= self.text.next()

print(self.image_text)

                text_file =

os.path.join(TEXT_DIR, self.tex_file)

self.TextCtrl
fh = open(text_file)

self.txt.SetValue(fh.read())

Yes, but without the typos.



    def loadTextFile(self, event):

        self.text_file = self.text.next()

        path = os.path.join(TEXT_DIR, self.text_file)

        self.txt.SetValue( open(path).read() )
-- Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.