1

这个 python27 32bit 代码在 IDLE 中完美运行,但是当被 py2exe 打包时......不太好。这不是缺少的 Tk() 问题的重复。运行 py2exe 生成的可执行文件时,其他一些东西似乎未初始化,ImageTk.PhotoImage() 处的调用因“创建图像太早”而中断:

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 - 非常基本的示例,是的,Tk() 已初始化。此外,模块版本似乎在 IDLE() 和可执行版本中都匹配

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 - 这是我的 py2exe 设置,我运行python setup.py py2exe以获取可执行文件:

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'}],
)

版本信息匹配,并且从 IDLE() 运行时打印图像会给出与可执行文件相同的值:

  • pil 3.4.2
  • tkinter $修订:81008 $
  • PIL.JpegImagePlugin.JpegImageFile 图像模式=RGB 大小=179x119 在 0x3DF6A50
4

1 回答 1

1

当一切都失败时,重建一切......

  1. 卸载32位python27.12
  2. 安装 32bit python27.10 // 12 也可能工作
  3. 点安装点
  4. 点安装枕头
  5. 安装 MSVC9
  6. 点安装py2exe
  7. 从旧的 py2exe 中删除所有以前的发行版本
  8. 重新生成可执行文件

有用!

于 2016-12-08T07:56:20.610 回答