2016-12-08 2 views
1

Ce code python27 32 bits fonctionne parfaitement dans IDLE, mais lorsqu'il est empaqueté par py2exe ... pas si bon. Ce n'est pas un doublon de la question Tk() manquante. Quelque chose d'autre semble être non initialisé lors de l'exécution du py2exe généré exécutable, l'appel à ImageTk.PhotoImage() avec renâcle « trop tôt pour créer l'image »:py2exe donne RuntimeError: Trop tôt pour créer l'image

C:\python\python_ui\exe\dist>basic.exe 
Traceback (most recent call last): 
    File "basic.py", line 7, in <module> 
    File "PIL\ImageTk.pyo", line 117, in __init__ 
    File "Tkinter.pyo", line 3367, in __init__ 
    File "Tkinter.pyo", line 3304, in __init__ 
RuntimeError: Too early to create image 
Exception AttributeError: "'PhotoImage' object has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <PIL.ImageTk.Ph 
otoImage object at 0x02CA3A90>> ignored 

basic.py - exemple très basique et oui, Tk() est initialisé. En outre, les versions du module semblent correspondre à la fois IDLE() et la version exécutable

from Tkinter import * 
from PIL import Image, ImageTk 

root = Tk() 

image = Image.open("background.jpg") 
photo = ImageTk.PhotoImage(image) 

label = Label(image=photo) 
label.image = photo # keep a reference! 
label.pack() 

root.mainloop() 

setup.py - Voici ma configuration py2exe, et je lance python setup.py py2exe pour obtenir l'exécutable:

import py2exe, sys, os 
from distutils.core import setup 
from glob import glob 

sys.path.append("C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\redist\\x86\\Microsoft.VC90.CRT") 
sys.argv.append('py2exe') 
setup( 
    data_files = [ 
     ("Microsoft.VC90.CRT", glob(r'C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*')), 
     ("background.jpg"), 
     ], 
    options = {   
    'py2exe' : { 
     'compressed': 1, 
     'optimize': 2, 
     'bundle_files': 3, 
     'dist_dir': 'dist', 
     'dll_excludes': ["MSVCP90.dll"] 
     } 
    },     
    zipfile=None, 
    console = [{'script':'user_code.py'}, {'script':'basic.py'}], 
) 

version correspond à l'information et l'impression de l'image donne les mêmes valeurs lors de son exécution de IDLE() comme il le fait l'exécutable:

  • pil 3.4.2
  • tkinter $ Revision: 81008 $
  • PIL.JpegImagePlugin.JpegImageFile mode image = Taille RVB = 179x119 à 0x3DF6A50
+0

essayé oreiller roulant arrière plusieurs versions, arrêté à 2,5 ... pas de chance – charo

Répondre

1

Quand tout le reste échoue, tout reconstruire ...

  1. Désinstaller 32bit python27.12
  2. installer // 32bit python27.10 12 ne fonctionnera probablement trop
  3. pip pip installer
  4. pip installer oreiller
  5. installer MSVC9
  6. pip installer py2exe
  7. supprimer toute distribution avant de construit plus py2exe
  8. Régénérer exécutable

Il fonctionne!