Re: image files packaged into single file executalble py pyinstaller

"Brendan Simon (eTRIX)" <[email protected]>
Newsgroups gmane.comp.python.wxglade
Organization eTRIX pty ltd
Message-ID <[email protected]>
On 3/11/17 6:27 pm, Johan Vromans wrote:
> On Thu, 2 Nov 2017 23:14:13 +1100, "Brendan Simon (eTRIX)"
> <[email protected]> wrote:
>> I want to build my wxpython app as a Mac app bundle, using
>> pyinstaller.  I can get it to run ok on my PC, because the images
>> that were created via wxglade actually exist on my Mac.  i.e. the
>> hardcoded absolute paths that wxglade generates will only work on my
>> Mac.
> I don't know about python, but I resolved this in Perl by overriding
> the Wx::Bitmap::new method. Not nice, but effective :)

Thanks Johan.  I took some inspiration from this and came up with the
following for Python.  It works by remapping `wx.Bitmap` to my custom
derived class.

Let me know if there are any issues with it, or if there is a better way
of doing things.

Put a, `import wxglade_pyinstaller` statement in the wxglade extra code
section for the bitmap, and created the following module called
"wxglade_pyinstaller.py"

    """
    Various utilities and helpers to handle pyinstaller issues.
    """

    import wx
    import sys
    import os

    ## taken from stackoverflow re accessing data files within
    pyinstaller bundle.
    def resource_path(relative_path):
        """Get absolute path to resource, works for dev and for
    PyInstaller."""
        base_path = getattr(sys, '_MEIPASS',
    os.path.dirname(os.path.abspath(__file__)))
        return os.path.join(base_path, relative_path)

    ## get a reference to original wx.Bitmap (just in case)
    wxBitmap = wx.Bitmap

    ## My customised wx.Bitmap, which obtains the location of the bitmap
    ## using the `resource_path` function above.
    class MyBitmap(wxBitmap):
        def __init__(self, *args, **kwargs):
            try:
                kwargs['name'] = resource_path(kwargs['name'])
            except KeyError:
                args = list(args)
                args[0] = resource_path(args[0])
            ## call original wx.Bitmap
            wxBitmap.__init__(self, *args, **kwargs)

    ## Remap wx.Bitmap to our customised version.
    wx.Bitmap = MyBitmap

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

_______________________________________________
wxGlade-general mailing list
wxGlade-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/wxglade-general
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.