determine App instance

Would you please let me know how to store an image inside an application, either in one/many frames or in the object TheApp? I thought it was only possible to load the image from an existing file any time it was needed. Thanks.

···

2007/12/15, Mark Erbaugh mark@microenh.com:

On Sat, 2007-12-15 at 10:16 -0800, Christopher Barker wrote:

Is there a built-in mechanism for a wx.Frame to determine the wx.App
instance?

yes –

TheApp = wx.GetApp
()

And you can use it anywhere – it’s in the wx module, not a Frame
method.

I’m considering using the wx.App instance to store global data,
particularly some bitmaps for wx.BitmapButton’s.

Are those same bitmaps used in multiple places? If not, I’d store
them
where they are used, but if so, then that’s a fine solution.

Yes, these are bitmaps for buttons that will be used on multiple frames

to give the app a consistent appearance.

Thanks for the wx.GetApp() pointer

Mark


To unsubscribe, e-mail:
wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Raffaello Barella wrote:

Would you please let me know how to store an image inside an application, either in one/many frames or in the object TheApp? I thought it was only possible to load the image from an existing file any time it was needed.

A couple options:

1) take a look at the img2py utility that comes with wxPython. It takes an image and stores it in a python module that can then be loaded, and the image extracted as either a wx.Image or wx.Bitmap.

2) IN your wx.App OnInit() (or anywhere, really) you do something like:

self.AParticularBitmap - wx.Bitmap(TheFileName)

Now you have a reference to that bitmap that you can use anywhere you need one.

My tendency would be to put all of these sorts of things in a "Resource'" module or package that you import and use anywhere you need it.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

I see others have already answered your question, but here is what I am
doing.

I use img2py.py to convert the image to a string. img2py.py should be in
the wx/tools folder. As someone else mentioned, I don't use the
individual loaders, but just the string and wrote a generic loader:

from wx import ImageFromStream
import cStringIO, zlib

def makeImage(data):
    return ImageFromStream(cStringIO.StringIO(zlib.decompress(data)))

# up arrow for list sort indication
up_img = makeImage( ...string from img2py... )

# down arrow for list sort indiciation
down_img = makeImage( ...string from img2py... )

etc.

I keep these in a separate file. I suspect that if my image library for
an application grows, I may split them into multiple files.

Then in my application main file I have:

from app.view.buttonGraphics import search_img,\
    preview_img, role_img, note_img, add_img, edit_img, delete_img,\
    up_img, down_img

class DemoApp(wx.App):

    def OnInit(self):
        self.InitBitmaps()
  ...

    def InitBitmaps(self):
        # button bitmaps
        # sort
        self.up_bm = wx.BitmapFromImage(up_img)
        self.down_bm = wx.BitmapFromImage(down_img)

I can now access my button bitmaps as members of the wx.App instance.

The reason that I don't create the bitmaps in the file with the image
strings is that wx.BitmapFromImage requires that the wx.App instance be
created first.

Mark

···

On Sun, 2007-12-16 at 07:05 +0100, Raffaello Barella wrote:

Would you please let me know how to store an image inside an
application, either in one/many frames or in the object TheApp? I
thought it was only possible to load the image from an existing file
any time it was needed. Thanks.

Mark Erbaugh wrote:

I see others have already answered your question, but here is what I am
doing.

all good stuff, until ---

Then in my application main file I have:

from app.view.buttonGraphics import search_img,\
    preview_img, role_img, note_img, add_img, edit_img, delete_img,\
    up_img, down_img

When I see and import line like that, I think -- yikes!

Remember, "Namespaces are one honking great idea. Let's do more of those!"

I much prefer something like:

from app.view import buttonGraphics

...

        self.up_bm = wx.BitmapFromImage(buttonGraphics.up_img)

It just seems so much cleaner...

If that still feels like too much typing, you can use "import ... as" to give buttonGraphics a shorter name, like "BG"

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Mark Erbaugh wrote:

···

On Sun, 2007-12-16 at 07:05 +0100, Raffaello Barella wrote:

Would you please let me know how to store an image inside an
application, either in one/many frames or in the object TheApp? I
thought it was only possible to load the image from an existing file
any time it was needed. Thanks.

I see others have already answered your question, but here is what I am
doing.

I use img2py.py to convert the image to a string. img2py.py should be in
the wx/tools folder. As someone else mentioned, I don't use the
individual loaders, but just the string and wrote a generic loader:

There is a patch being discussed over on wxPython-dev that will change how the embedded image code is generated. After the patch is done the embedded images will be instances of a class that has GetBitmap and etc. methods. There will also be a command-line flag that will turn on a compatibility mode if desired, and then there will be aliases for the old get[Name]Bitmap and etc. functions.

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Chris,

Thank you for the reply. You raise a good point.

Actually my code is a little different that what I actually said.

# -----------------------------------------
# file app.view.main_app.py

import wx
from app.view.buttonGraphics import search_img, ...

class SimplERP(wx.App):
  ...

def run():
    app = SimpleERP(False)
    app.MainLoop()

# ----------------------------------------
# file run.py

#! /usr/bin/env python

from app.view.main_app import run

run()

# ---------------------------------------

With this approach, aren't search_img, etc just imported into the
app.view.main_app namespace?

The reason I do this two-step launch is that when I have a finished
version, I run the app with python -OO (which generates .pyo files). I
then compress all the .pyo files in the app folder into a single app.zip
archive.

I can then run the app with a startup script:

# ---------------------------------------
# file run_zip.py

#! /usr/bin/env python

import sys

sys.path.insert(0, 'app.zip')

from app.view.main_app import run

run()

Mark

···

On Sun, 2007-12-16 at 22:12 -0800, Christopher Barker wrote:

> Then in my application main file I have:
>
> from app.view.buttonGraphics import search_img,\
> preview_img, role_img, note_img, add_img, edit_img, delete_img,\
> up_img, down_img

When I see and import line like that, I think -- yikes!

Remember, "Namespaces are one honking great idea. Let's do more of those!"

I much prefer something like:

from app.view import buttonGraphics

...

> self.up_bm = wx.BitmapFromImage(buttonGraphics.up_img)

It just seems so much cleaner...

If that still feels like too much typing, you can use "import ... as" to
give buttonGraphics a shorter name, like "BG"

Mark Erbaugh wrote:

# file app.view.main_app.py

import wx
from app.view.buttonGraphics import search_img, ...

With this approach, aren't search_img, etc just imported into the
app.view.main_app namespace?

yes. There really aren't truly global variables in Python the way there are in other languages -- and that's a good thing!

Even if it just for that module -- I still feel like it's kind of ugly -- and you now have to change code in two places if you add new bitmap -- once in your code where you use it, and once in the import statement.

Also, in the code itself it's not obvious where that variable came from.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception