Home > py2exe > Py2exe: Integrare immagini

Py2exe: Integrare immagini

4 Luglio 2011

Innanzitutto, sarebbe bene tenere le immagini relative al nostro
“programma”, tutte in una directory dedicata, nel mio caso
‘images’

Supponiamo che il “programma”, crei un frame con un bitmapButton,
il quale dovrà correttamente puntare al percorso dell’immagine,
che ingloba al suo interno:

#!/usr/bin/python
'''Simple Frame with BitmapButton'''

import wx
from wx.lib.buttons import GenBitmapTextButton

class FrameCore(wx.Frame):
    '''Initial Core Frame'''
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        panel = wx.Panel(self, wx.ID_ANY)

        self.btn_exit = GenBitmapTextButton(panel, wx.ID_ANY,
                                            wx.Bitmap('images\q.png'),
                                            'Exit'.rjust(15),
                                            (5, 5), (125, -1))
        self.btn_exit.SetBezelWidth(1)
        self.btn_exit.SetBackgroundColour('#c2e6f8')

        self.btn_exit.Bind(wx.EVT_ENTER_WINDOW, self.on_btn_enter)
        self.btn_exit.Bind(wx.EVT_LEAVE_WINDOW, self.on_btn_leave)
        self.btn_exit.Bind(wx.EVT_BUTTON, self.on_exit)

        self.Centre()
        self.Show()

    def on_btn_enter(self, event):
        '''Into Button enter-mouse event handler'''
        obj =  event.GetEventObject()
        obj.SetBackgroundColour('#ffdf85')
        obj.Refresh()

    def on_btn_leave(self, event):
        '''From Button leave-mouse event handler'''
        obj =  event.GetEventObject()
        obj.SetBackgroundColour('#c2e6f8')
        obj.Refresh()

    def on_exit(self, event):
        '''Close Frame'''
        self.Close()

def main_core(parent):
    '''Info frame starter'''
    app = wx.PySimpleApp()
    FrameCore(parent, -1, title  = 'Foo', size = (150, 100))
    app.MainLoop()
    
if __name__ == '__main__':
    main_core(parent = None)

Al momento del freeze del programma, dovremo indicare le immagini
e la directory che le conterrà, con lo stesso nome che abbiamo
usato in fase di sviluppo (‘images’):

...
    path = ("c:\Documents and Settings\banchellia\workspace\rubrica\src" +
            "\images\")
    data_files = [('images', [png for png in glob.glob(path + '*.png')])]

    setup(data_files = data_files,
          options = setup_opts,
          zipfile = None,
          windows = [test_wx],
        )
...

terminato il “freeze”, possiamo utilizzare l’eseguibile, con tanto di immagini e
nessun errore del tipo:

wx._core.PyAssertionError: C++ assertion "bmp.Ok()" failed at ....srcmswdib.cpp(148) in wxDIB::Create(): wxDIB::Create(): invalid bitmap

link:
il mio sample py2exepy2exe

Categorie:py2exe Tag:
  1. 21 Luglio 2011 a 13:31 | #1

    Se non hai niente al contrario,voglio usare questa informazione. Sara molto utile per il mio progetto e certo che io lascio il link del tuo post per far la gente sapere dove andare.

  2. 21 Luglio 2011 a 13:40 | #2

    ci mancherebbe! Buon lavoro.
    ciao.

I commenti sono chiusi.